<!doctype html>
<html>
<head>
<meta charset='UTF-8'><meta name='viewport' content='width=device-width initial-scale=1'>

<link href='https://fonts.loli.net/css?family=Open+Sans:400italic,700italic,700,400&subset=latin,latin-ext' rel='stylesheet' type='text/css' /><style type='text/css'>html {overflow-x: initial !important;}:root { --bg-color:#ffffff; --text-color:#333333; --select-text-bg-color:#B5D6FC; --select-text-font-color:auto; --monospace:"Lucida Console",Consolas,"Courier",monospace; --title-bar-height:20px; }
.mac-os-11 { --title-bar-height:28px; }
html { font-size: 14px; background-color: var(--bg-color); color: var(--text-color); font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; }
body { margin: 0px; padding: 0px; height: auto; inset: 0px; font-size: 1rem; line-height: 1.42857; overflow-x: hidden; background: inherit; tab-size: 4; }
iframe { margin: auto; }
a.url { word-break: break-all; }
a:active, a:hover { outline: 0px; }
.in-text-selection, ::selection { text-shadow: none; background: var(--select-text-bg-color); color: var(--select-text-font-color); }
#write { margin: 0px auto; height: auto; width: inherit; word-break: normal; overflow-wrap: break-word; position: relative; white-space: normal; overflow-x: visible; padding-top: 36px; }
#write.first-line-indent p { text-indent: 2em; }
#write.first-line-indent li p, #write.first-line-indent p * { text-indent: 0px; }
#write.first-line-indent li { margin-left: 2em; }
.for-image #write { padding-left: 8px; padding-right: 8px; }
body.typora-export { padding-left: 30px; padding-right: 30px; }
.typora-export .footnote-line, .typora-export li, .typora-export p { white-space: pre-wrap; }
.typora-export .task-list-item input { pointer-events: none; }
@media screen and (max-width: 500px) {
  body.typora-export { padding-left: 0px; padding-right: 0px; }
  #write { padding-left: 20px; padding-right: 20px; }
  .CodeMirror-sizer { margin-left: 0px !important; }
  .CodeMirror-gutters { display: none !important; }
}
#write li > figure:last-child { margin-bottom: 0.5rem; }
#write ol, #write ul { position: relative; }
img { max-width: 100%; vertical-align: middle; image-orientation: from-image; }
button, input, select, textarea { color: inherit; font: inherit; }
input[type="checkbox"], input[type="radio"] { line-height: normal; padding: 0px; }
*, ::after, ::before { box-sizing: border-box; }
#write h1, #write h2, #write h3, #write h4, #write h5, #write h6, #write p, #write pre { width: inherit; }
#write h1, #write h2, #write h3, #write h4, #write h5, #write h6, #write p { position: relative; }
p { line-height: inherit; }
h1, h2, h3, h4, h5, h6 { break-after: avoid-page; break-inside: avoid; orphans: 4; }
p { orphans: 4; }
h1 { font-size: 2rem; }
h2 { font-size: 1.8rem; }
h3 { font-size: 1.6rem; }
h4 { font-size: 1.4rem; }
h5 { font-size: 1.2rem; }
h6 { font-size: 1rem; }
.md-math-block, .md-rawblock, h1, h2, h3, h4, h5, h6, p { margin-top: 1rem; margin-bottom: 1rem; }
.hidden { display: none; }
.md-blockmeta { color: rgb(204, 204, 204); font-weight: 700; font-style: italic; }
a { cursor: pointer; }
sup.md-footnote { padding: 2px 4px; background-color: rgba(238, 238, 238, 0.7); color: rgb(85, 85, 85); border-radius: 4px; cursor: pointer; }
sup.md-footnote a, sup.md-footnote a:hover { color: inherit; text-transform: inherit; text-decoration: inherit; }
#write input[type="checkbox"] { cursor: pointer; width: inherit; height: inherit; }
figure { overflow-x: auto; margin: 1.2em 0px; max-width: calc(100% + 16px); padding: 0px; }
figure > table { margin: 0px; }
tr { break-inside: avoid; break-after: auto; }
thead { display: table-header-group; }
table { border-collapse: collapse; border-spacing: 0px; width: 100%; overflow: auto; break-inside: auto; text-align: left; }
table.md-table td { min-width: 32px; }
.CodeMirror-gutters { border-right: 0px; background-color: inherit; }
.CodeMirror-linenumber { user-select: none; }
.CodeMirror { text-align: left; }
.CodeMirror-placeholder { opacity: 0.3; }
.CodeMirror pre { padding: 0px 4px; }
.CodeMirror-lines { padding: 0px; }
div.hr:focus { cursor: none; }
#write pre { white-space: pre-wrap; }
#write.fences-no-line-wrapping pre { white-space: pre; }
#write pre.ty-contain-cm { white-space: normal; }
.CodeMirror-gutters { margin-right: 4px; }
.md-fences { font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; overflow: visible; white-space: pre; background: inherit; position: relative !important; }
.md-fences-adv-panel { width: 100%; margin-top: 10px; text-align: center; padding-top: 0px; padding-bottom: 8px; overflow-x: auto; }
#write .md-fences.mock-cm { white-space: pre-wrap; }
.md-fences.md-fences-with-lineno { padding-left: 0px; }
#write.fences-no-line-wrapping .md-fences.mock-cm { white-space: pre; overflow-x: auto; }
.md-fences.mock-cm.md-fences-with-lineno { padding-left: 8px; }
.CodeMirror-line, twitterwidget { break-inside: avoid; }
.footnotes { opacity: 0.8; font-size: 0.9rem; margin-top: 1em; margin-bottom: 1em; }
.footnotes + .footnotes { margin-top: 0px; }
.md-reset { margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: top; background: 0px 0px; text-decoration: none; text-shadow: none; float: none; position: static; width: auto; height: auto; white-space: nowrap; cursor: inherit; -webkit-tap-highlight-color: transparent; line-height: normal; font-weight: 400; text-align: left; box-sizing: content-box; direction: ltr; }
li div { padding-top: 0px; }
blockquote { margin: 1rem 0px; }
li .mathjax-block, li p { margin: 0.5rem 0px; }
li blockquote { margin: 1rem 0px; }
li { margin: 0px; position: relative; }
blockquote > :last-child { margin-bottom: 0px; }
blockquote > :first-child, li > :first-child { margin-top: 0px; }
.footnotes-area { color: rgb(136, 136, 136); margin-top: 0.714rem; padding-bottom: 0.143rem; white-space: normal; }
#write .footnote-line { white-space: pre-wrap; }
@media print {
  body, html { border: 1px solid transparent; height: 99%; break-after: avoid; break-before: avoid; font-variant-ligatures: no-common-ligatures; }
  #write { margin-top: 0px; padding-top: 0px; border-color: transparent !important; }
  .typora-export * { -webkit-print-color-adjust: exact; }
  .typora-export #write { break-after: avoid; }
  .typora-export #write::after { height: 0px; }
  .is-mac table { break-inside: avoid; }
  .typora-export-show-outline .typora-export-sidebar { display: none; }
}
.footnote-line { margin-top: 0.714em; font-size: 0.7em; }
a img, img a { cursor: pointer; }
pre.md-meta-block { font-size: 0.8rem; min-height: 0.8rem; white-space: pre-wrap; background: rgb(204, 204, 204); display: block; overflow-x: hidden; }
p > .md-image:only-child:not(.md-img-error) img, p > img:only-child { display: block; margin: auto; }
#write.first-line-indent p > .md-image:only-child:not(.md-img-error) img { left: -2em; position: relative; }
p > .md-image:only-child { display: inline-block; width: 100%; }
#write .MathJax_Display { margin: 0.8em 0px 0px; }
.md-math-block { width: 100%; }
.md-math-block:not(:empty)::after { display: none; }
.MathJax_ref { fill: currentcolor; }
[contenteditable="true"]:active, [contenteditable="true"]:focus, [contenteditable="false"]:active, [contenteditable="false"]:focus { outline: 0px; box-shadow: none; }
.md-task-list-item { position: relative; list-style-type: none; }
.task-list-item.md-task-list-item { padding-left: 0px; }
.md-task-list-item > input { position: absolute; top: 0px; left: 0px; margin-left: -1.2em; margin-top: calc(1em - 10px); border: none; }
.math { font-size: 1rem; }
.md-toc { min-height: 3.58rem; position: relative; font-size: 0.9rem; border-radius: 10px; }
.md-toc-content { position: relative; margin-left: 0px; }
.md-toc-content::after, .md-toc::after { display: none; }
.md-toc-item { display: block; color: rgb(65, 131, 196); }
.md-toc-item a { text-decoration: none; }
.md-toc-inner:hover { text-decoration: underline; }
.md-toc-inner { display: inline-block; cursor: pointer; }
.md-toc-h1 .md-toc-inner { margin-left: 0px; font-weight: 700; }
.md-toc-h2 .md-toc-inner { margin-left: 2em; }
.md-toc-h3 .md-toc-inner { margin-left: 4em; }
.md-toc-h4 .md-toc-inner { margin-left: 6em; }
.md-toc-h5 .md-toc-inner { margin-left: 8em; }
.md-toc-h6 .md-toc-inner { margin-left: 10em; }
@media screen and (max-width: 48em) {
  .md-toc-h3 .md-toc-inner { margin-left: 3.5em; }
  .md-toc-h4 .md-toc-inner { margin-left: 5em; }
  .md-toc-h5 .md-toc-inner { margin-left: 6.5em; }
  .md-toc-h6 .md-toc-inner { margin-left: 8em; }
}
a.md-toc-inner { font-size: inherit; font-style: inherit; font-weight: inherit; line-height: inherit; }
.footnote-line a:not(.reversefootnote) { color: inherit; }
.md-attr { display: none; }
.md-fn-count::after { content: "."; }
code, pre, samp, tt { font-family: var(--monospace); }
kbd { margin: 0px 0.1em; padding: 0.1em 0.6em; font-size: 0.8em; color: rgb(36, 39, 41); background: rgb(255, 255, 255); border: 1px solid rgb(173, 179, 185); border-radius: 3px; box-shadow: rgba(12, 13, 14, 0.2) 0px 1px 0px, rgb(255, 255, 255) 0px 0px 0px 2px inset; white-space: nowrap; vertical-align: middle; }
.md-comment { color: rgb(162, 127, 3); opacity: 0.8; font-family: var(--monospace); }
code { text-align: left; vertical-align: initial; }
a.md-print-anchor { white-space: pre !important; border-width: initial !important; border-style: none !important; border-color: initial !important; display: inline-block !important; position: absolute !important; width: 1px !important; right: 0px !important; outline: 0px !important; background: 0px 0px !important; text-decoration: initial !important; text-shadow: initial !important; }
.os-windows.monocolor-emoji .md-emoji { font-family: "Segoe UI Symbol", sans-serif; }
.md-diagram-panel > svg { max-width: 100%; }
[lang="flow"] svg, [lang="mermaid"] svg { max-width: 100%; height: auto; }
[lang="mermaid"] .node text { font-size: 1rem; }
table tr th { border-bottom: 0px; }
video { max-width: 100%; display: block; margin: 0px auto; }
iframe { max-width: 100%; width: 100%; border: none; }
.highlight td, .highlight tr { border: 0px; }
mark { background: rgb(255, 255, 0); color: rgb(0, 0, 0); }
.md-html-inline .md-plain, .md-html-inline strong, mark .md-inline-math, mark strong { color: inherit; }
.md-expand mark .md-meta { opacity: 0.3 !important; }
mark .md-meta { color: rgb(0, 0, 0); }
@media print {
  .typora-export h1, .typora-export h2, .typora-export h3, .typora-export h4, .typora-export h5, .typora-export h6 { break-inside: avoid; }
}
.md-diagram-panel .messageText { stroke: none !important; }
.md-diagram-panel .start-state { fill: var(--node-fill); }
.md-diagram-panel .edgeLabel rect { opacity: 1 !important; }
.md-require-zoom-fix { height: auto; margin-top: 16px; margin-bottom: 16px; }
.md-require-zoom-fix foreignobject { font-size: var(--mermaid-font-zoom); }
.md-fences.md-fences-math { font-size: 1em; }
.md-fences-advanced:not(.md-focus) { padding: 0px; white-space: nowrap; border: 0px; }
.md-fences-advanced:not(.md-focus) { background: inherit; }
.typora-export-show-outline .typora-export-content { max-width: 1440px; margin: auto; display: flex; flex-direction: row; }
.typora-export-sidebar { width: 300px; font-size: 0.8rem; margin-top: 80px; margin-right: 18px; }
.typora-export-show-outline #write { --webkit-flex:2; flex: 2 1 0%; }
.typora-export-sidebar .outline-content { position: fixed; top: 0px; max-height: 100%; overflow: hidden auto; padding-bottom: 30px; padding-top: 60px; width: 300px; }
@media screen and (max-width: 1024px) {
  .typora-export-sidebar, .typora-export-sidebar .outline-content { width: 240px; }
}
@media screen and (max-width: 800px) {
  .typora-export-sidebar { display: none; }
}
.outline-content li, .outline-content ul { margin-left: 0px; margin-right: 0px; padding-left: 0px; padding-right: 0px; list-style: none; }
.outline-content ul { margin-top: 0px; margin-bottom: 0px; }
.outline-content strong { font-weight: 400; }
.outline-expander { width: 1rem; height: 1.42857rem; position: relative; display: table-cell; vertical-align: middle; cursor: pointer; padding-left: 4px; }
.outline-expander::before { content: ""; position: relative; font-family: Ionicons; display: inline-block; font-size: 8px; vertical-align: middle; }
.outline-item { padding-top: 3px; padding-bottom: 3px; cursor: pointer; }
.outline-expander:hover::before { content: ""; }
.outline-h1 > .outline-item { padding-left: 0px; }
.outline-h2 > .outline-item { padding-left: 1em; }
.outline-h3 > .outline-item { padding-left: 2em; }
.outline-h4 > .outline-item { padding-left: 3em; }
.outline-h5 > .outline-item { padding-left: 4em; }
.outline-h6 > .outline-item { padding-left: 5em; }
.outline-label { cursor: pointer; display: table-cell; vertical-align: middle; text-decoration: none; color: inherit; }
.outline-label:hover { text-decoration: underline; }
.outline-item:hover { border-color: rgb(245, 245, 245); background-color: var(--item-hover-bg-color); }
.outline-item:hover { margin-left: -28px; margin-right: -28px; border-left: 28px solid transparent; border-right: 28px solid transparent; }
.outline-item-single .outline-expander::before, .outline-item-single .outline-expander:hover::before { display: none; }
.outline-item-open > .outline-item > .outline-expander::before { content: ""; }
.outline-children { display: none; }
.info-panel-tab-wrapper { display: none; }
.outline-item-open > .outline-children { display: block; }
.typora-export .outline-item { padding-top: 1px; padding-bottom: 1px; }
.typora-export .outline-item:hover { margin-right: -8px; border-right: 8px solid transparent; }
.typora-export .outline-expander::before { content: "+"; font-family: inherit; top: -1px; }
.typora-export .outline-expander:hover::before, .typora-export .outline-item-open > .outline-item > .outline-expander::before { content: "−"; }
.typora-export-collapse-outline .outline-children { display: none; }
.typora-export-collapse-outline .outline-item-open > .outline-children, .typora-export-no-collapse-outline .outline-children { display: block; }
.typora-export-no-collapse-outline .outline-expander::before { content: "" !important; }
.typora-export-show-outline .outline-item-active > .outline-item .outline-label { font-weight: 700; }
.md-inline-math-container mjx-container { zoom: 0.95; }


.CodeMirror { height: auto; }
.CodeMirror.cm-s-inner { background: inherit; }
.CodeMirror-scroll { overflow: auto hidden; z-index: 3; }
.CodeMirror-gutter-filler, .CodeMirror-scrollbar-filler { background-color: rgb(255, 255, 255); }
.CodeMirror-gutters { border-right: 1px solid rgb(221, 221, 221); background: inherit; white-space: nowrap; }
.CodeMirror-linenumber { padding: 0px 3px 0px 5px; text-align: right; color: rgb(153, 153, 153); }
.cm-s-inner .cm-keyword { color: rgb(119, 0, 136); }
.cm-s-inner .cm-atom, .cm-s-inner.cm-atom { color: rgb(34, 17, 153); }
.cm-s-inner .cm-number { color: rgb(17, 102, 68); }
.cm-s-inner .cm-def { color: rgb(0, 0, 255); }
.cm-s-inner .cm-variable { color: rgb(0, 0, 0); }
.cm-s-inner .cm-variable-2 { color: rgb(0, 85, 170); }
.cm-s-inner .cm-variable-3 { color: rgb(0, 136, 85); }
.cm-s-inner .cm-string { color: rgb(170, 17, 17); }
.cm-s-inner .cm-property { color: rgb(0, 0, 0); }
.cm-s-inner .cm-operator { color: rgb(152, 26, 26); }
.cm-s-inner .cm-comment, .cm-s-inner.cm-comment { color: rgb(170, 85, 0); }
.cm-s-inner .cm-string-2 { color: rgb(255, 85, 0); }
.cm-s-inner .cm-meta { color: rgb(85, 85, 85); }
.cm-s-inner .cm-qualifier { color: rgb(85, 85, 85); }
.cm-s-inner .cm-builtin { color: rgb(51, 0, 170); }
.cm-s-inner .cm-bracket { color: rgb(153, 153, 119); }
.cm-s-inner .cm-tag { color: rgb(17, 119, 0); }
.cm-s-inner .cm-attribute { color: rgb(0, 0, 204); }
.cm-s-inner .cm-header, .cm-s-inner.cm-header { color: rgb(0, 0, 255); }
.cm-s-inner .cm-quote, .cm-s-inner.cm-quote { color: rgb(0, 153, 0); }
.cm-s-inner .cm-hr, .cm-s-inner.cm-hr { color: rgb(153, 153, 153); }
.cm-s-inner .cm-link, .cm-s-inner.cm-link { color: rgb(0, 0, 204); }
.cm-negative { color: rgb(221, 68, 68); }
.cm-positive { color: rgb(34, 153, 34); }
.cm-header, .cm-strong { font-weight: 700; }
.cm-del { text-decoration: line-through; }
.cm-em { font-style: italic; }
.cm-link { text-decoration: underline; }
.cm-error { color: red; }
.cm-invalidchar { color: red; }
.cm-constant { color: rgb(38, 139, 210); }
.cm-defined { color: rgb(181, 137, 0); }
div.CodeMirror span.CodeMirror-matchingbracket { color: rgb(0, 255, 0); }
div.CodeMirror span.CodeMirror-nonmatchingbracket { color: rgb(255, 34, 34); }
.cm-s-inner .CodeMirror-activeline-background { background: inherit; }
.CodeMirror { position: relative; overflow: hidden; }
.CodeMirror-scroll { height: 100%; outline: 0px; position: relative; box-sizing: content-box; background: inherit; }
.CodeMirror-sizer { position: relative; }
.CodeMirror-gutter-filler, .CodeMirror-hscrollbar, .CodeMirror-scrollbar-filler, .CodeMirror-vscrollbar { position: absolute; z-index: 6; display: none; outline: 0px; }
.CodeMirror-vscrollbar { right: 0px; top: 0px; overflow: hidden; }
.CodeMirror-hscrollbar { bottom: 0px; left: 0px; overflow: auto hidden; }
.CodeMirror-scrollbar-filler { right: 0px; bottom: 0px; }
.CodeMirror-gutter-filler { left: 0px; bottom: 0px; }
.CodeMirror-gutters { position: absolute; left: 0px; top: 0px; padding-bottom: 10px; z-index: 3; overflow-y: hidden; }
.CodeMirror-gutter { white-space: normal; height: 100%; box-sizing: content-box; padding-bottom: 30px; margin-bottom: -32px; display: inline-block; }
.CodeMirror-gutter-wrapper { position: absolute; z-index: 4; background: 0px 0px !important; border: none !important; }
.CodeMirror-gutter-background { position: absolute; top: 0px; bottom: 0px; z-index: 4; }
.CodeMirror-gutter-elt { position: absolute; cursor: default; z-index: 4; }
.CodeMirror-lines { cursor: text; }
.CodeMirror pre { border-radius: 0px; border-width: 0px; background: 0px 0px; font-family: inherit; font-size: inherit; margin: 0px; white-space: pre; overflow-wrap: normal; color: inherit; z-index: 2; position: relative; overflow: visible; }
.CodeMirror-wrap pre { overflow-wrap: break-word; white-space: pre-wrap; word-break: normal; }
.CodeMirror-code pre { border-right: 30px solid transparent; width: fit-content; }
.CodeMirror-wrap .CodeMirror-code pre { border-right: none; width: auto; }
.CodeMirror-linebackground { position: absolute; inset: 0px; z-index: 0; }
.CodeMirror-linewidget { position: relative; z-index: 2; overflow: auto; }
.CodeMirror-wrap .CodeMirror-scroll { overflow-x: hidden; }
.CodeMirror-measure { position: absolute; width: 100%; height: 0px; overflow: hidden; visibility: hidden; }
.CodeMirror-measure pre { position: static; }
.CodeMirror div.CodeMirror-cursor { position: absolute; visibility: hidden; border-right: none; width: 0px; }
.CodeMirror div.CodeMirror-cursor { visibility: hidden; }
.CodeMirror-focused div.CodeMirror-cursor { visibility: inherit; }
.cm-searching { background: rgba(255, 255, 0, 0.4); }
span.cm-underlined { text-decoration: underline; }
span.cm-strikethrough { text-decoration: line-through; }
.cm-tw-syntaxerror { color: rgb(255, 255, 255); background-color: rgb(153, 0, 0); }
.cm-tw-deleted { text-decoration: line-through; }
.cm-tw-header5 { font-weight: 700; }
.cm-tw-listitem:first-child { padding-left: 10px; }
.cm-tw-box { border-style: solid; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-color: inherit; border-top-width: 0px !important; }
.cm-tw-underline { text-decoration: underline; }
@media print {
  .CodeMirror div.CodeMirror-cursor { visibility: hidden; }
}


:root {
    --side-bar-bg-color: #fafafa;
    --control-text-color: #777;
}

@include-when-export url(https://fonts.loli.net/css?family=Open+Sans:400italic,700italic,700,400&subset=latin,latin-ext);

/* open-sans-regular - latin-ext_latin */
  /* open-sans-italic - latin-ext_latin */
    /* open-sans-700 - latin-ext_latin */
    /* open-sans-700italic - latin-ext_latin */
  html {
    font-size: 16px;
    -webkit-font-smoothing: antialiased;
}

body {
    font-family: "Open Sans","Clear Sans", "Helvetica Neue", Helvetica, Arial, 'Segoe UI Emoji', sans-serif;
    color: rgb(51, 51, 51);
    line-height: 1.6;
}

#write {
    max-width: 860px;
  	margin: 0 auto;
  	padding: 30px;
    padding-bottom: 100px;
}

@media only screen and (min-width: 1400px) {
	#write {
		max-width: 1024px;
	}
}

@media only screen and (min-width: 1800px) {
	#write {
		max-width: 1200px;
	}
}

#write > ul:first-child,
#write > ol:first-child{
    margin-top: 30px;
}

a {
    color: #4183C4;
}
h1,
h2,
h3,
h4,
h5,
h6 {
    position: relative;
    margin-top: 1rem;
    margin-bottom: 1rem;
    font-weight: bold;
    line-height: 1.4;
    cursor: text;
}
h1:hover a.anchor,
h2:hover a.anchor,
h3:hover a.anchor,
h4:hover a.anchor,
h5:hover a.anchor,
h6:hover a.anchor {
    text-decoration: none;
}
h1 tt,
h1 code {
    font-size: inherit;
}
h2 tt,
h2 code {
    font-size: inherit;
}
h3 tt,
h3 code {
    font-size: inherit;
}
h4 tt,
h4 code {
    font-size: inherit;
}
h5 tt,
h5 code {
    font-size: inherit;
}
h6 tt,
h6 code {
    font-size: inherit;
}
h1 {
    font-size: 2.25em;
    line-height: 1.2;
    border-bottom: 1px solid #eee;
}
h2 {
    font-size: 1.75em;
    line-height: 1.225;
    border-bottom: 1px solid #eee;
}

/*@media print {
    .typora-export h1,
    .typora-export h2 {
        border-bottom: none;
        padding-bottom: initial;
    }

    .typora-export h1::after,
    .typora-export h2::after {
        content: "";
        display: block;
        height: 100px;
        margin-top: -96px;
        border-top: 1px solid #eee;
    }
}*/

h3 {
    font-size: 1.5em;
    line-height: 1.43;
}
h4 {
    font-size: 1.25em;
}
h5 {
    font-size: 1em;
}
h6 {
   font-size: 1em;
    color: #777;
}
p,
blockquote,
ul,
ol,
dl,
table{
    margin: 0.8em 0;
}
li>ol,
li>ul {
    margin: 0 0;
}
hr {
    height: 2px;
    padding: 0;
    margin: 16px 0;
    background-color: #e7e7e7;
    border: 0 none;
    overflow: hidden;
    box-sizing: content-box;
}

li p.first {
    display: inline-block;
}
ul,
ol {
    padding-left: 30px;
}
ul:first-child,
ol:first-child {
    margin-top: 0;
}
ul:last-child,
ol:last-child {
    margin-bottom: 0;
}
blockquote {
    border-left: 4px solid #dfe2e5;
    padding: 0 15px;
    color: #777777;
}
blockquote blockquote {
    padding-right: 0;
}
table {
    padding: 0;
    word-break: initial;
}
table tr {
    border: 1px solid #dfe2e5;
    margin: 0;
    padding: 0;
}
table tr:nth-child(2n),
thead {
    background-color: #f8f8f8;
}
table th {
    font-weight: bold;
    border: 1px solid #dfe2e5;
    border-bottom: 0;
    margin: 0;
    padding: 6px 13px;
}
table td {
    border: 1px solid #dfe2e5;
    margin: 0;
    padding: 6px 13px;
}
table th:first-child,
table td:first-child {
    margin-top: 0;
}
table th:last-child,
table td:last-child {
    margin-bottom: 0;
}

.CodeMirror-lines {
    padding-left: 4px;
}

.code-tooltip {
    box-shadow: 0 1px 1px 0 rgba(0,28,36,.3);
    border-top: 1px solid #eef2f2;
}

.md-fences,
code,
tt {
    border: 1px solid #e7eaed;
    background-color: #f8f8f8;
    border-radius: 3px;
    padding: 0;
    padding: 2px 4px 0px 4px;
    font-size: 0.9em;
}

code {
    background-color: #f3f4f4;
    padding: 0 2px 0 2px;
}

.md-fences {
    margin-bottom: 15px;
    margin-top: 15px;
    padding-top: 8px;
    padding-bottom: 6px;
}


.md-task-list-item > input {
  margin-left: -1.3em;
}

@media print {
    html {
        font-size: 13px;
    }
    table,
    pre {
        page-break-inside: avoid;
    }
    pre {
        word-wrap: break-word;
    }
}

.md-fences {
	background-color: #f8f8f8;
}
#write pre.md-meta-block {
	padding: 1rem;
    font-size: 85%;
    line-height: 1.45;
    background-color: #f7f7f7;
    border: 0;
    border-radius: 3px;
    color: #777777;
    margin-top: 0 !important;
}

.mathjax-block>.code-tooltip {
	bottom: .375rem;
}

.md-mathjax-midline {
    background: #fafafa;
}

#write>h3.md-focus:before{
	left: -1.5625rem;
	top: .375rem;
}
#write>h4.md-focus:before{
	left: -1.5625rem;
	top: .285714286rem;
}
#write>h5.md-focus:before{
	left: -1.5625rem;
	top: .285714286rem;
}
#write>h6.md-focus:before{
	left: -1.5625rem;
	top: .285714286rem;
}
.md-image>.md-meta {
    /*border: 1px solid #ddd;*/
    border-radius: 3px;
    padding: 2px 0px 0px 4px;
    font-size: 0.9em;
    color: inherit;
}

.md-tag {
    color: #a7a7a7;
    opacity: 1;
}

.md-toc { 
    margin-top:20px;
    padding-bottom:20px;
}

.sidebar-tabs {
    border-bottom: none;
}

#typora-quick-open {
    border: 1px solid #ddd;
    background-color: #f8f8f8;
}

#typora-quick-open-item {
    background-color: #FAFAFA;
    border-color: #FEFEFE #e5e5e5 #e5e5e5 #eee;
    border-style: solid;
    border-width: 1px;
}

/** focus mode */
.on-focus-mode blockquote {
    border-left-color: rgba(85, 85, 85, 0.12);
}

header, .context-menu, .megamenu-content, footer{
    font-family: "Segoe UI", "Arial", sans-serif;
}

.file-node-content:hover .file-node-icon,
.file-node-content:hover .file-node-open-state{
    visibility: visible;
}

.mac-seamless-mode #typora-sidebar {
    background-color: #fafafa;
    background-color: var(--side-bar-bg-color);
}

.md-lang {
    color: #b4654d;
}

/*.html-for-mac {
    --item-hover-bg-color: #E6F0FE;
}*/

#md-notification .btn {
    border: 0;
}

.dropdown-menu .divider {
    border-color: #e5e5e5;
    opacity: 0.4;
}

.ty-preferences .window-content {
    background-color: #fafafa;
}

.ty-preferences .nav-group-item.active {
    color: white;
    background: #999;
}

.menu-item-container a.menu-style-btn {
    background-color: #f5f8fa;
    background-image: linear-gradient( 180deg , hsla(0, 0%, 100%, 0.8), hsla(0, 0%, 100%, 0)); 
}



</style><title>umqtt</title>
</head>
<body class='typora-export os-windows'><div class='typora-export-content'>
<div id='write'  class=''><div class='md-toc' mdtype='toc'><p class="md-toc-content" role="list"><span role="listitem" class="md-toc-item md-toc-h2" data-ref="n3"><a class="md-toc-inner" href="#1-mqtt-背景应用"><strong>1. MQTT 背景应用</strong></a></span><span role="listitem" class="md-toc-item md-toc-h2" data-ref="n34"><a class="md-toc-inner" href="#2-mqtt-报文结构"><strong>2. MQTT 报文结构</strong></a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n51"><a class="md-toc-inner" href="#21-fixed-header"><strong>2.1. Fixed header</strong></a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n55"><a class="md-toc-inner" href="#211-mqtt-control-packet-type">2.1.1 MQTT Control Packet type</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n143"><a class="md-toc-inner" href="#212-flags-specific-to-each-mqtt-control-packet-type">2.1.2 Flags specific to each MQTT Control Packet type</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n260"><a class="md-toc-inner" href="#213-remaining-length">2.1.3 Remaining Length</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n292"><a class="md-toc-inner" href="#22-variable-header"><strong>2.2. Variable header</strong></a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n378"><a class="md-toc-inner" href="#23-payload"><strong>2.3. Payload</strong></a></span><span role="listitem" class="md-toc-item md-toc-h2" data-ref="n427"><a class="md-toc-inner" href="#3-mqtt报文格式"><strong>3. MQTT报文格式</strong></a></span><span role="listitem" class="md-toc-item md-toc-h2" data-ref="n438"><a class="md-toc-inner" href="#4-umqtt的实现"><strong>4. uMQTT的实现</strong></a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n456"><a class="md-toc-inner" href="#41-umqtt的结构框架"><strong>4.1. uMQTT的结构框架</strong></a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n468"><a class="md-toc-inner" href="#42-umqtt客户端"><strong>4.2. uMQTT客户端</strong></a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n491"><a class="md-toc-inner" href="#43-umqtt与lwip"><strong>4.3. uMQTT与LWIP</strong></a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n503"><a class="md-toc-inner" href="#44-umqtt发送组包"><strong>4.4. uMQTT发送组包</strong></a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n537"><a class="md-toc-inner" href="#45-umqtt接收解包"><strong>4.5. uMQTT接收解包</strong></a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n571"><a class="md-toc-inner" href="#46-umqtt数据流向"><strong>4.6. uMQTT数据流向</strong></a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n576"><a class="md-toc-inner" href="#461-umqtttransconnect-函数">4.6.1 umqtt_trans_connect 函数</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n578"><a class="md-toc-inner" href="#462-umqttencode-函数">4.6.2 umqtt_encode 函数</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n580"><a class="md-toc-inner" href="#463-umqtthandlereadpacket函数">4.6.3 umqtt_handle_readpacket函数</a></span><span role="listitem" class="md-toc-item md-toc-h2" data-ref="n582"><a class="md-toc-inner" href="#5-扩展"><strong>5. 扩展</strong></a></span></p></div><p>&nbsp;</p><h2 id='1-mqtt-背景应用'><strong><span>1. MQTT 背景应用</span></strong></h2><p><strong><span>MQTT</span></strong><span>是机器对机器(</span><code>M2M</code><span>)/物联网(</span><code>IoT</code><span>)连接协议，英文全名为&quot;</span><strong><span>Message Queuing Telemetry Transport</span></strong><span>&quot;，中文译名为“</span><strong><span>消息队列遥测传输</span></strong><span>”协议。它是专为受限设备和低带宽、高延迟或不可靠的网络而设计的，是一种基于</span><code>发布</code><span>/</span><code>订阅</code><span>（</span><code>publish</code><span>/</span><code>subscribe</code><span>）模式的&quot;轻量级&quot;通讯协议，该协议构建于</span><code>TCP/IP</code><span>协议之上，由IBM在1999年发布。目前，该协议的最新版本为V5.0，常用版本为V3.1.1。</span></p><p><span>举例说明MQTT的实际应用场景。如图1 - 1，很好地展示出了一个基于MQTT协议的通信网络系统案例：</span>
<img src="./umqtt.assets/frame.jpg" referrerpolicy="no-referrer" alt="frame"></p><center>图1 - 1	基于MQTT的通信案例</center><p><span>名词释义：</span></p><ul><li><strong><span>Publisher</span></strong><span> - 发布者</span></li><li><strong><span>Broker</span></strong><span> - 代理（服务端）</span></li><li><strong><span>Subscriber</span></strong><span> - 订阅者</span></li><li><strong><span>Topic</span></strong><span> - 发布/订阅的主题</span></li></ul><p><span>流程概述：上图中，各类传感器的角色是发布者(Publisher)。譬如，湿度传感器和温度传感器分别向接入的MQTT Broker中（周期性）发布两个主题名为&quot;Moisture&quot;（湿度）和&quot;Temp&quot;（温度）的主题；当然，伴随着这两个主题共同发布的，还有湿度值和温度值，被称为“消息”。几个客户端的角色是订阅者Subscriber，如手机APP从Broker订阅了&quot;Temp&quot;主题，便能在手机上获取到温度传感器Publish在Broker中的温度值。</span></p><p><span>补充说明：</span></p><ol start='' ><li><span>发布者和订阅者的角色并非是固定的，而是相对的。发布者也可以同时从Broker订阅主题，同理，订阅者也可以向Broker发布主题；即发布者可以是订阅者，订阅者也可以是发布者。</span></li><li><span>Broker可以是在线的云服务器，也可以是本地搭建的局域网客户端；按照需求，实际上Broker自身也会包含一些订阅/发布主题的功能。</span></li></ol><p><span>更多参考资料，请前往</span><a href='http://mqtt.p2hp.com/'><span>MQTT中文网</span></a><span>或</span><a href='https://mqtt.org/'><span>MQTT官网</span></a><span>查阅。</span></p><hr /><p><span>学习目标：</span></p><ol start='' ><li><span>了解MQTT协议及其报文格式</span></li><li><span>理解uMQTT和LWIP的关系</span></li><li><span>掌握uMQTT的实现原理</span></li></ol><h2 id='2-mqtt-报文结构'><strong><span>2. MQTT 报文结构</span></strong></h2><p><span>任何通用/私有协议都是由事先规定好的、按某种规则约束的各种报文数据包组成的，MQTT也不例外。在MQTT协议中，所有的数据包都是由最多三部分组成：</span><code>固定header</code><span> + </span><code>可变header</code><span> + </span><code>有效载荷</code><span>，如表2 - 1：</span></p><figure><table><thead><tr><th><strong><a href='#21-fixed-header'><span>Fixed header</span></a><span>（at least 2 Bytes）</span></strong></th><th><a href='#22-variable-header'><span>Variable header</span></a></th><th style='text-align:left;' ><a href='#23-payload'><span>Payload</span></a></th></tr></thead><tbody><tr><td><strong><span>存在于所有MQTT控制数据包中</span></strong></td><td><span>存在于部分MQTT控制数据包中</span></td><td style='text-align:left;' ><span>存在于部分MQTT控制数据包中</span></td></tr><tr><td><span>Bytes[0]    Bytes[1]...</span></td><td><span>... ...</span></td><td style='text-align:left;' ><span>...Bytes[N-1]    Bytes[N]</span></td></tr></tbody></table></figure><center>表2 - 1	MQTT的数据包组成格式</center><p><span>其中，固定Header是必需的，可变Header和有效载荷是非必需的。因此，理论上来说，MQTT协议数据包的最小长度为2个字节，造就了它本身占用的额外资源消耗最小化特色。接下来将以MQTT3.1.1为例，来介绍各部分报文的详细格式（详细用法见 </span><a href='#4-umqtt的实现'><strong><span>4. uMQTT的实现</span></strong></a><span>）。</span></p><h3 id='21-fixed-header'><strong><span>2.1. Fixed header</span></strong></h3><table>
    <tbody><tr>
        <td align="center"><b>Bit</b></td>
        <td align="center"><b>7</b></td>
        <td align="center"><b>6</b></td>
        <td align="center"><b>5</b></td>
        <td align="center"><b>4</b></td>
        <td align="center"><b>3</b></td>
        <td align="center"><b>2</b></td>
        <td align="center"><b>1</b></td>
        <td align="center"><b>0</b></td>
    </tr>
    <tr>
        <td>Bytes[0]</td>
        <td colspan="4">MQTT控制报文类型(MQTT Control Packet type)</td>
        <td colspan="4">MQTT控制报文类型标志位(Flags specific to each MQTT Control Packet type)</td>
    </tr>
    <tr>
        <td>Bytes[1]...</td>
        <td colspan="8" ,="" align="center">剩余长度(Remaining Length)</td>
</tr></tbody></table>
<center>表2 - 2 Fixed header format</center><p><span>固定Header由至少两个字节组成，如表2 - 2。第一个字节的高4位描述了当前数据报文的类型(</span><a href='#211-mqtt-control-packet-type'><span>MQTT Control Packet type</span></a><span>)，见下表2 - 3；低四位定义了与报文类型相关的标志位(</span><a href='#212-flags-specific-to-each-mqtt-control-packet-type'><span>Flags specific to each MQTT Control Packet type</span></a><span>)，见下文表2 - 4；第二个及之后的至多4个字节代表着剩余数据的字节长度(</span><a href='#213-remaining-length'><span>Remaining Length</span></a><span>)，见下文表2 - 5。</span></p><h4 id='211-mqtt-control-packet-type'><span>2.1.1 MQTT Control Packet type</span></h4><figure><table><thead><tr><th style='text-align:center;' ><strong><span>名称</span></strong></th><th style='text-align:center;' ><strong><span>值</span></strong></th><th style='text-align:center;' ><strong><span>Bytes[0]</span></strong></th><th style='text-align:center;' ><strong><span>描述</span></strong></th></tr></thead><tbody><tr><td style='text-align:center;' ><span>Reserved</span></td><td style='text-align:center;' ><span>0</span></td><td style='text-align:center;' ><span>0x0*</span></td><td style='text-align:center;' ><span>预留位</span></td></tr><tr><td style='text-align:center;' ><span>CONNECT</span></td><td style='text-align:center;' ><span>1</span></td><td style='text-align:center;' ><span>0x1*</span></td><td style='text-align:center;' ><span>客户端请求连接</span></td></tr><tr><td style='text-align:center;' ><span>CONNACK</span></td><td style='text-align:center;' ><span>2</span></td><td style='text-align:center;' ><span>0x2*</span></td><td style='text-align:center;' ><span>服务端连接确认</span></td></tr><tr><td style='text-align:center;' ><span>PUBLISH</span></td><td style='text-align:center;' ><span>3</span></td><td style='text-align:center;' ><span>0x3*</span></td><td style='text-align:center;' ><span>发布消息</span></td></tr><tr><td style='text-align:center;' ><span>PUBACK</span></td><td style='text-align:center;' ><span>4</span></td><td style='text-align:center;' ><span>0x4*</span></td><td style='text-align:center;' ><span>发布确认（QoS1）</span></td></tr><tr><td style='text-align:center;' ><span>PUBREC</span></td><td style='text-align:center;' ><span>5</span></td><td style='text-align:center;' ><span>0x5*</span></td><td style='text-align:center;' ><span>发布收到（QoS2 - 保证交付第1部分）</span></td></tr><tr><td style='text-align:center;' ><span>PUBREL</span></td><td style='text-align:center;' ><span>6</span></td><td style='text-align:center;' ><span>0x6*</span></td><td style='text-align:center;' ><span>发布释放（QoS2 - 保证交付第2部分）</span></td></tr><tr><td style='text-align:center;' ><span>PUBCOMP</span></td><td style='text-align:center;' ><span>7</span></td><td style='text-align:center;' ><span>0x7*</span></td><td style='text-align:center;' ><span>发布完成（QoS2 - 保证交付第3部分）</span></td></tr><tr><td style='text-align:center;' ><span>SUBSCRIBE</span></td><td style='text-align:center;' ><span>8</span></td><td style='text-align:center;' ><span>0x8*</span></td><td style='text-align:center;' ><span>客户端订阅请求</span></td></tr><tr><td style='text-align:center;' ><span>SUBACK</span></td><td style='text-align:center;' ><span>9</span></td><td style='text-align:center;' ><span>0x9*</span></td><td style='text-align:center;' ><span>服务端订阅确认</span></td></tr><tr><td style='text-align:center;' ><span>UNSUBSCRIBE</span></td><td style='text-align:center;' ><span>10</span></td><td style='text-align:center;' ><span>0xA*</span></td><td style='text-align:center;' ><span>客户端取消订阅请求</span></td></tr><tr><td style='text-align:center;' ><span>UNSUBACK</span></td><td style='text-align:center;' ><span>11</span></td><td style='text-align:center;' ><span>0xB*</span></td><td style='text-align:center;' ><span>服务端取消订阅确认</span></td></tr><tr><td style='text-align:center;' ><span>PINGREQ</span></td><td style='text-align:center;' ><span>12</span></td><td style='text-align:center;' ><span>0xC*</span></td><td style='text-align:center;' ><span>客户端心跳(PING)请求</span></td></tr><tr><td style='text-align:center;' ><span>PINGRESP</span></td><td style='text-align:center;' ><span>13</span></td><td style='text-align:center;' ><span>0xD*</span></td><td style='text-align:center;' ><span>服务端心跳(PING)响应</span></td></tr><tr><td style='text-align:center;' ><span>DISCONNECT</span></td><td style='text-align:center;' ><span>14</span></td><td style='text-align:center;' ><span>0xE*</span></td><td style='text-align:center;' ><span>客户端即将断开连接</span></td></tr><tr><td style='text-align:center;' ><span>Reserved</span></td><td style='text-align:center;' ><span>15</span></td><td style='text-align:center;' ><span>0xF*</span></td><td style='text-align:center;' ><span>预留位</span></td></tr></tbody></table></figure><center>表2 - 3 Control Packet type</center><h4 id='212-flags-specific-to-each-mqtt-control-packet-type'><span>2.1.2 Flags specific to each MQTT Control Packet type</span></h4><figure><table><thead><tr><th><strong><span>控制报文类型</span></strong></th><th><strong><span>Fixed header flags</span></strong></th><th><strong><span>Bit 3</span></strong></th><th><strong><span>Bit 2</span></strong></th><th><strong><span>Bit 1</span></strong></th><th><strong><span>Bit 0</span></strong></th></tr></thead><tbody><tr><td><span>CONNECT</span></td><td><span>Reserved</span></td><td><span>0</span></td><td><span>0</span></td><td><span>0</span></td><td><span>0</span></td></tr><tr><td><span>CONNACK</span></td><td><span>Reserved</span></td><td><span>0</span></td><td><span>0</span></td><td><span>0</span></td><td><span>0</span></td></tr><tr><td><span>PUBLISH</span></td><td><span>Used in MQTT 3.1.1</span></td><td><span>DUP</span></td><td><span>QoS</span></td><td><span>QoS</span></td><td><span>RETAIN</span></td></tr><tr><td><span>PUBACK</span></td><td><span>Reserved</span></td><td><span>0</span></td><td><span>0</span></td><td><span>0</span></td><td><span>0</span></td></tr><tr><td><span>PUBREC</span></td><td><span>Reserved</span></td><td><span>0</span></td><td><span>0</span></td><td><span>0</span></td><td><span>0</span></td></tr><tr><td><span>PUBREL</span></td><td><span>Reserved</span></td><td><span>0</span></td><td><span>0</span></td><td><span>1</span></td><td><span>0</span></td></tr><tr><td><span>PUBCOMP</span></td><td><span>Reserved</span></td><td><span>0</span></td><td><span>0</span></td><td><span>0</span></td><td><span>0</span></td></tr><tr><td><span>SUBSCRIBE</span></td><td><span>Reserved</span></td><td><span>0</span></td><td><span>0</span></td><td><span>1</span></td><td><span>0</span></td></tr><tr><td><span>SUBACK</span></td><td><span>Reserved</span></td><td><span>0</span></td><td><span>0</span></td><td><span>0</span></td><td><span>0</span></td></tr><tr><td><span>UNSUBSCRIBE</span></td><td><span>Reserved</span></td><td><span>0</span></td><td><span>0</span></td><td><span>1</span></td><td><span>0</span></td></tr><tr><td><span>UNSUBACK</span></td><td><span>Reserved</span></td><td><span>0</span></td><td><span>0</span></td><td><span>0</span></td><td><span>0</span></td></tr><tr><td><span>PINGREQ</span></td><td><span>Reserved</span></td><td><span>0</span></td><td><span>0</span></td><td><span>0</span></td><td><span>0</span></td></tr><tr><td><span>PINGRESP</span></td><td><span>Reserved</span></td><td><span>0</span></td><td><span>0</span></td><td><span>0</span></td><td><span>0</span></td></tr><tr><td><span>DISCONNECT</span></td><td><span>Reserved</span></td><td><span>0</span></td><td><span>0</span></td><td><span>0</span></td><td><span>0</span></td></tr></tbody></table></figure><center>表2 - 4 Control Packet Flags</center><p><span>Fixed header中</span><strong><span>Bytes[0]</span></strong><span>的</span><strong><span>bit[3..0]</span></strong><span>包含了每个MQTT控制报文类型的特殊标识，如上表2 - 4。表中flags为“Reserved”的各标志位虽然是预留给将来版本使用的，但是在数据传输过程中必须赋值为表中的值。如果识别到了与表中不符的非法flag，接收方必须关闭网络连接。</span></p><p><span>其中，已经用在3.1.1版本中的3种标志位：</span></p><ul><li><span>DUP = 控制报文的重复分发标志[Duplicate]</span></li><li><span>QoS = PUBLISH报文的服务质量等级[Quality of Service]</span></li><li><span>RETAIN = PUBLISH报文的保留标志</span></li></ul><h4 id='213-remaining-length'><span>2.1.3 Remaining Length</span></h4><figure><table><thead><tr><th><strong><span>字节数</span></strong></th><th><strong><span>起</span></strong><span>(Bytes[1], Bytes[2], Bytes[3], Bytes[4])</span></th><th><strong><span>止(Bytes[1], Bytes[2], Bytes[3], Bytes[4])</span></strong></th></tr></thead><tbody><tr><td><span>1</span></td><td><span>0 (0x00)</span></td><td><span>127 (0x7F)</span></td></tr><tr><td><span>2</span></td><td><span>128 (0x80, 0x01)</span></td><td><span>16 383 (0xFF, 0x7F)</span></td></tr><tr><td><span>3</span></td><td><span>16 384 (0x80, 0x80, 0x01)</span></td><td><span>2 097 151 (0xFF, 0xFF, 0x7F)</span></td></tr><tr><td><span>4</span></td><td><span>2 097 152 (0x80, 0x80, 0x80, 0x01)</span></td><td><span>268 435 455 (0xFF, 0xFF, 0xFF, 0x7F)</span></td></tr></tbody></table></figure><center>表2 - 5 Remaining Length</center><p><span>剩余长度（Remaining Length）表示当前报文剩余部分的字节数，包括可变header和有效载荷的数据。剩余长度不包括用于编码剩余长度字段本身的字节数，即不包含Fixed header的长度。</span></p><p><span>剩余长度字段使用一个变长度编码方案，如上表2 - 5：</span></p><ul><li><span>小于128的值，使用单字节编码。</span></li><li><span>更大的值按下面的方式处理——低7位有效位用于编码数据，最高有效位用于指示是否有更多的字节。因此每个字节可以编码128个数值和一个</span><strong><span>延续位（continuation bit）</span></strong><span>。延续位仅有指示进位功能，在计算长度时不能被计算在内。</span></li><li><span>该字段最多4个字节。</span></li></ul><h3 id='22-variable-header'><strong><span>2.2. Variable header</span></strong></h3><p><span>某些MQTT控制报文包含一个可变header部分，它位于固定header和有效载荷之间。可变header的内容根据控制报文类型(</span><a href='#211-mqtt-control-packet-type'><span>MQTT Control Packet type</span></a><span>)的不同而不同；譬如</span><strong><span>CONNECT</span></strong><span>类型的报文，它的可变header含有“协议名称（Protocol Name）”、”协议等级（Protocol Level）“、”连接标志（Connect Flags）“和”保活间隔（Keep Alive）“四个字段域。其余可变header，详见</span><a href='#3-mqtt报文格式'><span>MQTT报文格式</span></a><span>。</span></p><p><span>虽然有各种不同的可变header内容，但是报文标识符（Packet Identifier）是一个重要的通用字段，且具有</span><code>唯一性</code><span>。见表2 - 6：</span></p><figure><table><thead><tr><th><strong><span>字节</span></strong></th><th><strong><span>值</span></strong></th></tr></thead><tbody><tr><td><span>byte 1</span></td><td><span>报文标识符 MSB</span></td></tr><tr><td><span>byte 2</span></td><td><span>报文标识符 LSB</span></td></tr></tbody></table></figure><center>表2 - 6 Packet Identifier</center><p><span>这个通用字段存在于多种类型的报文里，见表2 - 7：</span></p><figure><table><thead><tr><th><strong><span>报文类型</span></strong></th><th><strong><span>是否存在报文标识符</span></strong></th></tr></thead><tbody><tr><td><span>CONNECT</span></td><td><span>NO</span></td></tr><tr><td><span>CONNACK</span></td><td><span>NO</span></td></tr><tr><td><span>PUBLISH</span></td><td><span>YES (If QoS &gt; 0)</span></td></tr><tr><td><span>PUBACK</span></td><td><span>YES</span></td></tr><tr><td><span>PUBREC</span></td><td><span>YES</span></td></tr><tr><td><span>PUBREL</span></td><td><span>YES</span></td></tr><tr><td><span>PUBCOMP</span></td><td><span>YES</span></td></tr><tr><td><span>SUBSCRIBE</span></td><td><span>YES</span></td></tr><tr><td><span>SUBACK</span></td><td><span>YES</span></td></tr><tr><td><span>UNSUBSCRIBE</span></td><td><span>YES</span></td></tr><tr><td><span>UNSUBACK</span></td><td><span>YES</span></td></tr><tr><td><span>PINGREQ</span></td><td><span>NO</span></td></tr><tr><td><span>PINGRESP</span></td><td><span>NO</span></td></tr><tr><td><span>DISCONNECT</span></td><td><span>NO</span></td></tr></tbody></table></figure><center>表2 - 7 Control Packets that contain a Packet Identifier</center><p><span>报文标识符（Packet Identifier）的规则如下：</span></p><ul><li><p><span>SUBSCRIBE、UNSUBSCRIBE、和PUBLISH（在QoS&gt;0的情况下）控制数据包必须包含</span><strong><span>非零16位</span></strong><span>报文标识符。</span></p></li><li><p><span>每次客户端发送这些类型之一的新数据包时，它必须为其分配一个当前未使用的报文标识符。</span></p></li><li><p><span>如果一个客户端重新发送了某个特定的控制数据包，那么它必须在该数据包的后续重发中使用与之相同的报文标识符。在客户端处理了该特定控制数据包相对应的ACK数据包之后，该报文标识符得以重新使用。</span></p><ul><li><span>QoS 1的PUBLISH包，PUBACK与之对应。</span></li><li><span>QoS 2的PUBLISH包，PUBCOMP与之对应。</span></li><li><span>对于SUBSCRIBE 和 UNSUBSCRIBE，由SUBACK和UNSUBACK与之一一对应。</span></li></ul></li><li><p><span>当服务端发送QoS&gt;0的PUBLISH报文时，也应遵守上述规则。</span></p></li><li><p><span>QoS=0的PUBLISH报文，</span><strong><span>不允许</span></strong><span>包含报文标识符。</span></p></li><li><p><span>PUBACK，PUBREC，PUBREL包的报文标识符，必须和PUBLISH包最初发送的值相同。</span></p></li><li><p><span>类似地，SUBACK和UNSUBACK包，必须与SUBSCRIBE和UNSUBSCRIBE包中的报文标识符相同。</span></p></li></ul><p><span>客户端和服务端相互独立地分配报文标识符。因此，客户端-服务端组合使用相同的报文标识符，可以实现并发信息交换。</span></p><h3 id='23-payload'><strong><span>2.3. Payload</span></strong></h3><p><span>某些MQTT控制报文在数据包的最后部分包含有效载荷，详情见</span><a href='#3-mqtt报文格式'><span>MQTT报文格式</span></a><span>。例如，对于PUBLISH报文来说，有效载荷就可以是应用消息。表2 - 8列出了各种需要有效载荷的控制报文。</span></p><figure><table><thead><tr><th style='text-align:left;' ><strong><span>控制报文</span></strong></th><th style='text-align:left;' ><strong><span>有效载荷</span></strong></th></tr></thead><tbody><tr><td style='text-align:left;' ><span>CONNECT</span></td><td style='text-align:left;' ><span>Required</span></td></tr><tr><td style='text-align:left;' ><span>CONNACK</span></td><td style='text-align:left;' ><span>None</span></td></tr><tr><td style='text-align:left;' ><span>PUBLISH</span></td><td style='text-align:left;' ><span>Optional</span></td></tr><tr><td style='text-align:left;' ><span>PUBACK</span></td><td style='text-align:left;' ><span>None</span></td></tr><tr><td style='text-align:left;' ><span>PUBREC</span></td><td style='text-align:left;' ><span>None</span></td></tr><tr><td style='text-align:left;' ><span>PUBREL</span></td><td style='text-align:left;' ><span>None</span></td></tr><tr><td style='text-align:left;' ><span>PUBCOMP</span></td><td style='text-align:left;' ><span>None</span></td></tr><tr><td style='text-align:left;' ><span>SUBSCRIBE</span></td><td style='text-align:left;' ><span>Required</span></td></tr><tr><td style='text-align:left;' ><span>SUBACK</span></td><td style='text-align:left;' ><span>Required</span></td></tr><tr><td style='text-align:left;' ><span>UNSUBSCRIBE</span></td><td style='text-align:left;' ><span>Required</span></td></tr><tr><td style='text-align:left;' ><span>UNSUBACK</span></td><td style='text-align:left;' ><span>None</span></td></tr><tr><td style='text-align:left;' ><span>PINGREQ</span></td><td style='text-align:left;' ><span>None</span></td></tr><tr><td style='text-align:left;' ><span>PINGRESP</span></td><td style='text-align:left;' ><span>None</span></td></tr><tr><td style='text-align:left;' ><span>DISCONNECT</span></td><td style='text-align:left;' ><span>None</span></td></tr></tbody></table></figure><center>表2 - 8 Control Packets that contain a Payload</center><h2 id='3-mqtt报文格式'><strong><span>3. MQTT报文格式</span></strong></h2><p><span>由于Variable header和Payload的格式较多，本文不再赘述，详情可参考以下链接：</span></p><ul><li><a href='http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718027'><strong><span>MQTT Version 3.1.1</span></strong><span> | 3   MQTT Control Packets</span></a></li><li><a href='http://mqtt.p2hp.com/mqtt311'><span>MQTT 3.1.1 协议 中文版 | MQTT中文网</span></a></li><li><a href='https://github.com/mcxiaoke/mqtt/blob/master/mqtt/03-ControlPackets.md'><span>第三章 MQTT控制报文 MQTT Control Packets</span></a></li><li><a href='https://mcxiaoke.gitbook.io/mqtt/03-controlpackets'><span>第三章 – MQTT控制报文</span></a></li></ul><h2 id='4-umqtt的实现'><strong><span>4. uMQTT的实现</span></strong></h2><p><span>uMQTT 软件包是 RT-Thread 自主研发的，基于 MQTT 3.1.1 协议的客户端实现，它提供了设备与 MQTT Broker 通讯的基本功能。GitHub主页</span><a href='https://github.com/RT-Thread-packages/umqtt'><span>点击这里</span></a><span>，Gitee主页</span><a href='https://gitee.com/RT-Thread-Mirror/umqtt'><span>点这里</span></a><span>。</span></p><p><span>uMQTT 软件包功能如下:  </span></p><ul><li><span>实现基础的连接、订阅、发布功能;  </span></li><li><span>具备多重心跳保活，设备重连机制，保证 mqtt 在线状态，适应复杂情况; </span></li><li><span>支持 QoS=0, QoS=1, QoS=2 三种发送信息质量;  </span></li><li><span>支持多客户端使用;</span></li><li><span>用户端接口简便，留有多种对外回调函数;</span></li><li><span>支持多种技术参数可配置，易上手，便于产品化开发；</span></li><li><span>功能强大，资源占用率低，支持功能可裁剪。</span></li></ul><h3 id='41-umqtt的结构框架'><strong><span>4.1. uMQTT的结构框架</span></strong></h3><p><span>uMQTT 软件包主要用于在嵌入式设备上实现 MQTT 协议，软件包的主要工作基于 MQTT 协议实现。软件包结构框图如图4 - 1:</span></p><p><img src="./umqtt.assets/umqtt_分层图.jpg" referrerpolicy="no-referrer" alt="umqtt_分层图"></p><center>图4 - 1	uMQTT结构框图</center><p><span>软件包实现过程中主要做了:</span></p><ol start='' ><li><span>根据 MQTT 3.1.1 协议规定，进行软件包数据协议的封包解包；</span></li><li><span>传输层函数适配对接 SAL （Socket Abstraction Layer）层；</span></li><li><span>uMQTT 客户端层，根据协议包层和传输层编写符合应用层的接口。实现基础连接、断连、订阅、取消订阅、发布消息等功能。支持 QoS0/1/2 三种发送信息质量。利用 uplink timer 定时器，实现多重心跳保活机制和设备重连机制，增加设备在线稳定性，适应复杂情况。</span></li></ol><h3 id='42-umqtt客户端'><strong><span>4.2. uMQTT客户端</span></strong></h3><p><span>由图1 - 1可知，想要连接Broker，嵌入式设备需要作为MQTT协议中的客户端来使用。</span></p><p><span>在uMQTT组件的</span><code>umqtt.h</code><span>文件中，抽象出了初始化客户端用到的MQTT配置信息，组成对应的数据结构体：</span></p><pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="C" style="break-inside: unset;"><div class="CodeMirror cm-s-inner cm-s-null-scroll CodeMirror-wrap" lang="c"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 9.5px; left: 8px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">struct</span> <span class="cm-def">umqtt_info</span></span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">{</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">rt_size_t</span> <span class="cm-variable">send_size</span>, <span class="cm-variable">recv_size</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* 收发缓冲区大小 */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">const</span> <span class="cm-variable-3">char</span> <span class="cm-variable-3">*</span><span class="cm-variable">uri</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">/* 完整的URI (包含: URI + URN) */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">const</span> <span class="cm-variable-3">char</span> <span class="cm-variable-3">*</span><span class="cm-variable">client_id</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">/* 客户端ID */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">const</span> <span class="cm-variable-3">char</span> <span class="cm-variable-3">*</span><span class="cm-variable">lwt_topic</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">/* 遗嘱主题 */</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">const</span> <span class="cm-variable-3">char</span> <span class="cm-variable-3">*</span><span class="cm-variable">lwt_message</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">/* 遗嘱消息 */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">const</span> <span class="cm-variable-3">char</span> <span class="cm-variable-3">*</span><span class="cm-variable">user_name</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">/* 用户名 */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">const</span> <span class="cm-variable-3">char</span> <span class="cm-variable-3">*</span><span class="cm-variable">password</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* 密码 */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">enum</span> <span class="cm-variable">umqtt_qos</span> <span class="cm-variable">lwt_qos</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* 遗嘱QoS */</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">umqtt_subscribe_cb</span> <span class="cm-variable">lwt_cb</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">/* 遗嘱回调函数 */</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">rt_uint8_t</span> <span class="cm-variable">reconnect_max_num</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* 最大重连次数 */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">rt_uint32_t</span> <span class="cm-variable">reconnect_interval</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* 最大重连时间间隔 */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">rt_uint8_t</span> <span class="cm-variable">keepalive_max_num</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* 最大保活次数 */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">rt_uint32_t</span> <span class="cm-variable">keepalive_interval</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* 最大保活时间间隔 */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">rt_uint32_t</span> <span class="cm-variable">recv_time_ms</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* 接收超时时间 */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">rt_uint32_t</span> <span class="cm-variable">connect_time</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* 连接超时时间 */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">rt_uint32_t</span> <span class="cm-variable">send_timeout</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* 上行(发布/订阅/取消订阅)超时时间 */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">rt_uint32_t</span> <span class="cm-variable">thread_stack_size</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">/* 线程栈大小 */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">rt_uint8_t</span> <span class="cm-variable">thread_priority</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* 线程优先级 */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-meta">#ifdef PKG_UMQTT_TEST_SHORT_KEEPALIVE_TIME</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">rt_uint16_t</span> <span class="cm-variable">connect_keepalive_sec</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">/* 连接信息，保活秒数 */</span> &nbsp; &nbsp;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-meta">#endif</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">};</span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 552px;"></div><div class="CodeMirror-gutters" style="display: none; height: 552px;"></div></div></div></pre><p><span>这些配置信息一般在创建uMQTT客户端之前需要自行填写指定，譬如Broker的”URI“、”用户名“或”密码“之类的关键信息。其它的非关键信息，如果没有指定，那么会在创建客户端函数</span><code>umqtt_create</code><span>中，调用</span><code>umqtt_check_def_info</code><span>函数来赋值为默认值：</span></p><pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="C" style="break-inside: unset;"><div class="CodeMirror cm-s-inner cm-s-null-scroll CodeMirror-wrap" lang="c"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 9.5px; left: 8px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">static</span> <span class="cm-variable-3">void</span> <span class="cm-def">umqtt_check_def_info</span>(<span class="cm-keyword">struct</span> <span class="cm-def">umqtt_info</span> <span class="cm-operator">*</span><span class="cm-variable">info</span>)</span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">{</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">if</span> (<span class="cm-variable">info</span>) </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">if</span> (<span class="cm-variable">info</span><span class="cm-operator">-&gt;</span><span class="cm-variable">send_size</span> <span class="cm-operator">==</span> <span class="cm-number">0</span>) { <span class="cm-variable">info</span><span class="cm-operator">-&gt;</span><span class="cm-variable">send_size</span> <span class="cm-operator">=</span> <span class="cm-variable">PKG_UMQTT_INFO_DEF_SENDSIZE</span>; }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">if</span> (<span class="cm-variable">info</span><span class="cm-operator">-&gt;</span><span class="cm-variable">recv_size</span> <span class="cm-operator">==</span> <span class="cm-number">0</span>) { <span class="cm-variable">info</span><span class="cm-operator">-&gt;</span><span class="cm-variable">recv_size</span> <span class="cm-operator">=</span> <span class="cm-variable">PKG_UMQTT_INFO_DEF_RECVSIZE</span>; }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">if</span> (<span class="cm-variable">info</span><span class="cm-operator">-&gt;</span><span class="cm-variable">reconnect_max_num</span> <span class="cm-operator">==</span> <span class="cm-number">0</span>) { <span class="cm-variable">info</span><span class="cm-operator">-&gt;</span><span class="cm-variable">reconnect_max_num</span> <span class="cm-operator">=</span> <span class="cm-variable">PKG_UMQTT_INFO_DEF_RECONNECT_MAX_NUM</span>; }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">if</span> (<span class="cm-variable">info</span><span class="cm-operator">-&gt;</span><span class="cm-variable">reconnect_interval</span> <span class="cm-operator">==</span> <span class="cm-number">0</span>) { <span class="cm-variable">info</span><span class="cm-operator">-&gt;</span><span class="cm-variable">reconnect_interval</span> <span class="cm-operator">=</span> <span class="cm-variable">PKG_UMQTT_INFO_DEF_RECONNECT_INTERVAL</span>; }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">if</span> (<span class="cm-variable">info</span><span class="cm-operator">-&gt;</span><span class="cm-variable">keepalive_max_num</span> <span class="cm-operator">==</span> <span class="cm-number">0</span>) { <span class="cm-variable">info</span><span class="cm-operator">-&gt;</span><span class="cm-variable">keepalive_max_num</span> <span class="cm-operator">=</span> <span class="cm-variable">PKG_UMQTT_INFO_DEF_KEEPALIVE_MAX_NUM</span>; }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">if</span> (<span class="cm-variable">info</span><span class="cm-operator">-&gt;</span><span class="cm-variable">keepalive_interval</span> <span class="cm-operator">==</span> <span class="cm-number">0</span>) { <span class="cm-variable">info</span><span class="cm-operator">-&gt;</span><span class="cm-variable">keepalive_interval</span> <span class="cm-operator">=</span> <span class="cm-variable">PKG_UMQTT_INFO_DEF_HEARTBEAT_INTERVAL</span>; }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">if</span> (<span class="cm-variable">info</span><span class="cm-operator">-&gt;</span><span class="cm-variable">connect_time</span> <span class="cm-operator">==</span> <span class="cm-number">0</span>) { <span class="cm-variable">info</span><span class="cm-operator">-&gt;</span><span class="cm-variable">connect_time</span> <span class="cm-operator">=</span> <span class="cm-variable">PKG_UMQTT_INFO_DEF_CONNECT_TIMEOUT</span>; }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">if</span> (<span class="cm-variable">info</span><span class="cm-operator">-&gt;</span><span class="cm-variable">recv_time_ms</span> <span class="cm-operator">==</span> <span class="cm-number">0</span>) { <span class="cm-variable">info</span><span class="cm-operator">-&gt;</span><span class="cm-variable">recv_time_ms</span> <span class="cm-operator">=</span> <span class="cm-variable">PKG_UMQTT_INFO_DEF_RECV_TIMEOUT_MS</span>; }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">if</span> (<span class="cm-variable">info</span><span class="cm-operator">-&gt;</span><span class="cm-variable">send_timeout</span> <span class="cm-operator">==</span> <span class="cm-number">0</span>) { <span class="cm-variable">info</span><span class="cm-operator">-&gt;</span><span class="cm-variable">send_timeout</span> <span class="cm-operator">=</span> <span class="cm-variable">PKG_UMQTT_INFO_DEF_SEND_TIMEOUT</span>; }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">if</span> (<span class="cm-variable">info</span><span class="cm-operator">-&gt;</span><span class="cm-variable">thread_stack_size</span> <span class="cm-operator">==</span> <span class="cm-number">0</span>) { <span class="cm-variable">info</span><span class="cm-operator">-&gt;</span><span class="cm-variable">thread_stack_size</span> <span class="cm-operator">=</span> <span class="cm-variable">PKG_UMQTT_INFO_DEF_THREAD_STACK_SIZE</span>; }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">if</span> (<span class="cm-variable">info</span><span class="cm-operator">-&gt;</span><span class="cm-variable">thread_priority</span> <span class="cm-operator">==</span> <span class="cm-number">0</span>) { <span class="cm-variable">info</span><span class="cm-operator">-&gt;</span><span class="cm-variable">thread_priority</span> <span class="cm-operator">=</span> <span class="cm-variable">PKG_UMQTT_INFO_DEF_THREAD_PRIORITY</span>; }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 391px;"></div><div class="CodeMirror-gutters" style="display: none; height: 391px;"></div></div></div></pre><p><span>然而只有上述信息，是无法运行起来一个MQTT客户端的。故在</span><code>umqtt.c</code><span>中，含有</span><code>umqtt_info</code><span>的</span><code>umqtt_client</code><span>结构体列出了初始化客户端用到的所有数据：</span></p><pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="C" style="break-inside: unset;"><div class="CodeMirror cm-s-inner cm-s-null-scroll CodeMirror-wrap" lang="c"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 9.5px; left: 8px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><span><span>​</span>x</span></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">struct</span> <span class="cm-def">umqtt_client</span></span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">{</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable-3">int</span> <span class="cm-variable">sock</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* 套接字 */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">enum</span> <span class="cm-variable">umqtt_client_state</span> <span class="cm-variable">connect_state</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">/* mqtt客户端状态 */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">struct</span> <span class="cm-def">umqtt_info</span> <span class="cm-def">mqtt_info</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">/* mqtt用户配置信息 */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">rt_uint8_t</span> <span class="cm-variable">reconnect_count</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* mqtt客户端重连计数 */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">rt_uint8_t</span> <span class="cm-variable">keepalive_count</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* mqtt保活计数 */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">rt_uint32_t</span> <span class="cm-variable">pingreq_last_tick</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">/* mqtt的PING请求上一次滴答值 */</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">rt_uint32_t</span> <span class="cm-variable">uplink_next_tick</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* 上行连接的下一次滴答值 */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">rt_uint32_t</span> <span class="cm-variable">uplink_last_tick</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* 上行连接的上一次滴答值 */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">rt_uint32_t</span> <span class="cm-variable">reconnect_next_tick</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">/* 客户端断开重连时的下一次滴答值 */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">rt_uint32_t</span> <span class="cm-variable">reconnect_last_tick</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">/* 客户端断开重连时的上一次滴答值 */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">rt_uint8_t</span> <span class="cm-operator">*</span><span class="cm-variable">send_buf</span>, <span class="cm-operator">*</span><span class="cm-variable">recv_buf</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">/* 收发缓冲区指针 */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">rt_size_t</span> <span class="cm-variable">send_len</span>, <span class="cm-variable">recv_len</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* 收发数据的长度 */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">rt_uint16_t</span> <span class="cm-variable">packet_id</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">/* mqtt报文标识符 */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">rt_mutex_t</span> <span class="cm-variable">lock_client</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* mqtt客户端互斥锁 */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">rt_mq_t</span> <span class="cm-variable">msg_queue</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">/* mqtt客户端消息队列 */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">rt_timer_t</span> <span class="cm-variable">uplink_timer</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">/* mqtt保活重连定时器 */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable-3">int</span> <span class="cm-variable">sub_recv_list_len</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">/* 接收订阅信息的链表长度 */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">rt_list_t</span> <span class="cm-variable">sub_recv_list</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">/* 订阅消息的链表头 */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">rt_list_t</span> <span class="cm-variable">qos2_msg_list</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">/* QoS2的消息链表 */</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">struct</span> <span class="cm-def">umqtt_pubrec_msg</span> <span class="cm-def">pubrec_msg</span>[<span class="cm-variable">PKG_UMQTT_QOS2_QUE_MAX</span>]; <span class="cm-comment">/* 发布收到消息数组(QoS=2) */</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">umqtt_user_callback</span> <span class="cm-variable">user_handler</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* 用户句柄 */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable-3">void</span> <span class="cm-variable-3">*</span><span class="cm-variable">user_data</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">/* 用户数据 */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">rt_thread_t</span> <span class="cm-variable">task_handle</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">/* umqtt任务线程 */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">rt_list_t</span> <span class="cm-variable">list</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* umqtt链表头 */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">};</span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 851px;"></div><div class="CodeMirror-gutters" style="display: none; height: 851px;"></div></div></div></pre><p><span>上述部分成员的结构体和枚举类型定义，可自行在</span><code>umqtt.h</code><span>文件中查看。该结构体会在创建客户端函数</span><code>umqtt_create</code><span>中，调用</span><code>umqtt_check_def_info</code><span>函数之后初始化：</span></p><ol start='' ><li><span>初始化遗嘱数据结构（如果有的话）</span></li><li><span>为收发缓冲区申请内存</span></li><li><span>创建互斥锁、消息队列和超时重连定时器（超时回调实现重连+保活）</span></li><li><span>初始化各链表</span></li><li><span>创建</span><code>umqtt_thread</code><span>——mqtt数据收发线程</span></li><li><span>返回</span><code>mqtt_client</code><span>结构体地址</span></li></ol><p><span>当第6步返回的值不为空时，即可调用</span><code>umqtt_start</code><span>函数来通过</span><strong><span>LWIP</span></strong><span>发送</span><strong><span>CONNECT</span></strong><span>报文连接Broker；连接成功后便会启动</span><code>umqtt_thread</code><span>线程，开启MQTT的通信之路了。</span></p><h3 id='43-umqtt与lwip'><strong><span>4.3. uMQTT与LWIP</span></strong></h3><p><span>在</span><code>umqtt_start</code><span>函数中，首先会将uMQTT客户端的状态置为</span><code>UMQTT_CS_LINKING</code><span>，表示</span><code>正在连接中</code><span>。接下来会调用</span><code>umqtt_connect</code><span>函数，将本地客户端连接到Broker。</span></p><p><span>连接到Broker的过程分两步：</span></p><ol start='' ><li><span>创建</span><strong><span>套接字</span></strong><span>，与Broker建立链路连接</span></li><li><span>发送CONNECT报文，创建MQTT协议连接</span></li></ol><p><span>在</span><code>umqtt_connect</code><span>函数中，通过调用</span><code>umqtt_trans_connect</code><span>函数，来完成第一步：</span></p><pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="C" style="break-inside: unset;"><div class="CodeMirror cm-s-inner cm-s-null-scroll CodeMirror-wrap" lang="c"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 9.5px; left: 8px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment">/** </span></span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* TCP/TLS Connection Complete for configured transport</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">*</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* @param uri the input server URI address</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* @param sock the output socket</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">*</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* @return &lt;0: failed or other error</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* &nbsp; &nbsp; &nbsp; &nbsp; =0: success</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">*/</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable-3">int</span> <span class="cm-def">umqtt_trans_connect</span>(<span class="cm-keyword">const</span> <span class="cm-variable-3">char</span> <span class="cm-variable-3">*</span><span class="cm-variable">uri</span>, <span class="cm-variable-3">int</span> <span class="cm-variable-3">*</span><span class="cm-variable">sock</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">{</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable-3">int</span> <span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-number">0</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">struct</span> <span class="cm-def">addrinfo</span> <span class="cm-operator">*</span><span class="cm-variable">addr_res</span> <span class="cm-operator">=</span> <span class="cm-variable">RT_NULL</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-operator">*</span><span class="cm-variable">sock</span> <span class="cm-operator">=</span> <span class="cm-operator">-</span><span class="cm-number">1</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-comment">/* 域名解析 */</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">umqtt_resolve_uri</span>(<span class="cm-variable">uri</span>, <span class="cm-operator">&amp;</span><span class="cm-variable">addr_res</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">if</span> ((<span class="cm-variable">_ret</span> <span class="cm-operator">&lt;</span> <span class="cm-number">0</span>) <span class="cm-operator">||</span> (<span class="cm-variable">addr_res</span> <span class="cm-operator">==</span> <span class="cm-variable">RT_NULL</span>)) </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">LOG_E</span>(<span class="cm-string">"resolve uri err"</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">UMQTT_FAILED</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">goto</span> <span class="cm-variable">exit</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-comment">/* 创建套接字 */</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">if</span> ((<span class="cm-operator">*</span><span class="cm-variable">sock</span> <span class="cm-operator">=</span> <span class="cm-variable">socket</span>(<span class="cm-variable">addr_res</span><span class="cm-operator">-&gt;</span><span class="cm-variable">ai_family</span>, <span class="cm-variable">SOCK_STREAM</span>, <span class="cm-variable">UMQTT_SOCKET_PROTOCOL</span>)) <span class="cm-operator">&lt;</span> <span class="cm-number">0</span>) </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">LOG_E</span>(<span class="cm-string">"create socket error!"</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">UMQTT_FAILED</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">goto</span> <span class="cm-variable">exit</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-comment">/* 设置套接字工作在非阻塞模式下 */</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">ioctlsocket</span>(<span class="cm-operator">*</span><span class="cm-variable">sock</span>, <span class="cm-variable">FIONBIO</span>, <span class="cm-number">0</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">if</span> (<span class="cm-variable">_ret</span> <span class="cm-operator">&lt;</span> <span class="cm-number">0</span>) </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">LOG_E</span>(<span class="cm-string">" iocontrol socket error!"</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">UMQTT_FAILED</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">goto</span> <span class="cm-variable">exit</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-comment">/* 建立连接 */</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">if</span> ((<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">connect</span>(<span class="cm-operator">*</span><span class="cm-variable">sock</span>, <span class="cm-variable">addr_res</span><span class="cm-operator">-&gt;</span><span class="cm-variable">ai_addr</span>, <span class="cm-variable">addr_res</span><span class="cm-operator">-&gt;</span><span class="cm-variable">ai_addrlen</span>)) <span class="cm-operator">&lt;</span> <span class="cm-number">0</span>) </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">LOG_E</span>(<span class="cm-string">" connect err!"</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">closesocket</span>(<span class="cm-operator">*</span><span class="cm-variable">sock</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-operator">*</span><span class="cm-variable">sock</span> <span class="cm-operator">=</span> <span class="cm-operator">-</span><span class="cm-number">1</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">UMQTT_FAILED</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">goto</span> <span class="cm-variable">exit</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">exit</span>:</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">if</span> (<span class="cm-variable">addr_res</span>) {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">freeaddrinfo</span>(<span class="cm-variable">addr_res</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">addr_res</span> <span class="cm-operator">=</span> <span class="cm-variable">RT_NULL</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">return</span> <span class="cm-variable">_ret</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 1265px;"></div><div class="CodeMirror-gutters" style="display: none; height: 1265px;"></div></div></div></pre><p><span>这个函数，就是uMQTT通过LWIP与Broker建立连接的核心函数。并且从</span><a href='#41-umqtt的结构框架'><span>uMQTT的框架图</span></a><span>中我们可以知道，该函数是通过</span><strong><span>SAL</span></strong><span>即</span><strong><span>套接字抽象层</span></strong><span>组件，来调用相关接口访问LWIP的。用到的部分SAL组件封装的函数（</span><code>getaddrinfo</code><span>是在</span><code>umqtt_resolve_uri</code><span>函数中用来解析域名的）如下：</span></p><pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="c" style="break-inside: unset;"><div class="CodeMirror cm-s-inner cm-s-null-scroll CodeMirror-wrap" lang="c"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 9.5px; left: 8px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable-3">int</span> <span class="cm-def">getaddrinfo</span>(<span class="cm-keyword">const</span> <span class="cm-variable-3">char</span> <span class="cm-variable-3">*</span><span class="cm-variable">nodename</span>,</span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; <span class="cm-keyword">const</span> <span class="cm-variable-3">char</span> <span class="cm-variable-3">*</span><span class="cm-variable">servname</span>,</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; <span class="cm-keyword">const</span> <span class="cm-keyword">struct</span> <span class="cm-def">addrinfo</span> <span class="cm-operator">*</span><span class="cm-variable">hints</span>,</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; <span class="cm-keyword">struct</span> <span class="cm-def">addrinfo</span> <span class="cm-operator">**</span><span class="cm-variable">res</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">{</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">return</span> <span class="cm-variable">sal_getaddrinfo</span>(<span class="cm-variable">nodename</span>, <span class="cm-variable">servname</span>, <span class="cm-variable">hints</span>, <span class="cm-variable">res</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">}</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-operator">---------------------------------------------------------------------------------------------</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-meta">#define connect(s, name, namelen) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  sal_connect(s, name, namelen)</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-meta">#define recvfrom(s, mem, len, flags, from, fromlen) &nbsp; &nbsp; &nbsp;  sal_recvfrom(s, mem, len, flags, from, fromlen)</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-meta">#define send(s, dataptr, size, flags) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  sal_sendto(s, dataptr, size, flags, NULL, NULL)</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-meta">#define socket(domain, type, protocol) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sal_socket(domain, type, protocol)</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-meta">#define closesocket(s) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sal_closesocket(s)</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-meta">#define ioctlsocket(s, cmd, arg) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sal_ioctlsocket(s, cmd, arg)</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 322px;"></div><div class="CodeMirror-gutters" style="display: none; height: 322px;"></div></div></div></pre><h3 id='44-umqtt发送组包'><strong><span>4.4. uMQTT发送组包</span></strong></h3><p><span>当uMQTT客户端与Broker成功建立链路层连接后，就会立刻发送CONNECT报文，建立MQTT的协议层连接。</span></p><p><span>uMQTT组件使用了巧妙的结构体+共用体来管理所有的收发报文：</span></p><pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="C" style="break-inside: unset;"><div class="CodeMirror cm-s-inner cm-s-null-scroll CodeMirror-wrap" lang="c"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 9.5px; left: 8px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">union</span> <span class="cm-variable">umqtt_pkgs_msg</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">/* mqtt message packet type */</span> </span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">{</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">struct</span> <span class="cm-def">umqtt_pkgs_connect</span> &nbsp; &nbsp; <span class="cm-def">connect</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">/* connect */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">struct</span> <span class="cm-def">umqtt_pkgs_connack</span> &nbsp; &nbsp; <span class="cm-def">connack</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">/* connack */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">struct</span> <span class="cm-def">umqtt_pkgs_publish</span> &nbsp; &nbsp; <span class="cm-def">publish</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">/* publish */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">struct</span> <span class="cm-def">umqtt_pkgs_puback</span> &nbsp; &nbsp; &nbsp;<span class="cm-def">puback</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* puback */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">struct</span> <span class="cm-def">umqtt_pkgs_pubrec</span> &nbsp; &nbsp; &nbsp;<span class="cm-def">pubrec</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* publish receive (QoS 2, step_1st) */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">struct</span> <span class="cm-def">umqtt_pkgs_pubrel</span> &nbsp; &nbsp; &nbsp;<span class="cm-def">pubrel</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* publish release (QoS 2, step_2nd) */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">struct</span> <span class="cm-def">umqtt_pkgs_pubcomp</span> &nbsp; &nbsp; <span class="cm-def">pubcomp</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">/* publish complete (QoS 2, step_3rd) */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">struct</span> <span class="cm-def">umqtt_pkgs_subscribe</span> &nbsp; <span class="cm-def">subscribe</span>; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">/* subscribe topic */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">struct</span> <span class="cm-def">umqtt_pkgs_suback</span> &nbsp; &nbsp; &nbsp;<span class="cm-def">suback</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* subscribe ack */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">struct</span> <span class="cm-def">umqtt_pkgs_unsubscribe</span> <span class="cm-def">unsubscribe</span>; &nbsp; &nbsp; &nbsp;<span class="cm-comment">/* unsubscribe topic */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">struct</span> <span class="cm-def">umqtt_pkgs_unsuback</span> &nbsp; &nbsp;<span class="cm-def">unsuback</span>; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* unsubscribe ack */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">};</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">struct</span> <span class="cm-def">umqtt_msg</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">{</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">union</span> <span class="cm-variable">umqtt_pkgs_fix_header</span> <span class="cm-variable">header</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* fix header */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">rt_uint32_t</span> <span class="cm-variable">msg_len</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">/* message length */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">union</span> <span class="cm-variable">umqtt_pkgs_msg</span> <span class="cm-variable">msg</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* retain payload message */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">};</span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 483px;"></div><div class="CodeMirror-gutters" style="display: none; height: 483px;"></div></div></div></pre><p><span>该结构体的各种报文类型正好对应</span><a href='#211-mqtt-control-packet-type'><span>2.1.1章节</span></a><span>的各种控制报文类型（PINGREQ和PINGRESP的报文各自只需两个字节，</span><a href='http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718081'><span>参考此处</span></a><span>，因此没有必要使用结构体来管理）。通过</span><code>umqtt_encode</code><span>函数来调用不同的组包函数，填充对应格式的结构体，然后发送到Broker服务端：</span></p><pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="C" style="break-inside: unset;"><div class="CodeMirror cm-s-inner cm-s-null-scroll CodeMirror-wrap" lang="c"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 9.5px; left: 8px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment">/** </span></span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* packaging the data according to the format</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">*</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* @param type the input packaging type</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* @param send_buf the output send buf, result of the package</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* @param send_len the output send buffer length</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* @param message the input message</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">*</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* @return &lt;=0: failed or other error</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* &nbsp; &nbsp; &nbsp; &nbsp; &gt;0: package data length</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">*/</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable-3">int</span> <span class="cm-def">umqtt_encode</span>(<span class="cm-keyword">enum</span> <span class="cm-variable">umqtt_type</span> <span class="cm-variable">type</span>, <span class="cm-variable">rt_uint8_t</span> <span class="cm-operator">*</span><span class="cm-variable">send_buf</span>, <span class="cm-variable-3">size_t</span> <span class="cm-variable">send_len</span>, <span class="cm-keyword">struct</span> <span class="cm-def">umqtt_msg</span> <span class="cm-operator">*</span><span class="cm-variable">message</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">{</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable-3">int</span> <span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-number">0</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">switch</span> (<span class="cm-variable">type</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">case</span> <span class="cm-variable">UMQTT_TYPE_CONNECT</span>:</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">umqtt_connect_encode</span>(<span class="cm-variable">send_buf</span>, <span class="cm-variable">send_len</span>, <span class="cm-operator">&amp;</span>(<span class="cm-variable">message</span><span class="cm-operator">-&gt;</span><span class="cm-variable">msg</span>.<span class="cm-variable">connect</span>));</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">break</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">case</span> <span class="cm-variable">UMQTT_TYPE_PUBLISH</span>:</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">umqtt_publish_encode</span>(<span class="cm-variable">send_buf</span>, <span class="cm-variable">send_len</span>, <span class="cm-variable">message</span><span class="cm-operator">-&gt;</span><span class="cm-variable">header</span>.<span class="cm-variable">bits</span>.<span class="cm-variable">dup</span>, <span class="cm-variable">message</span><span class="cm-operator">-&gt;</span><span class="cm-variable">header</span>.<span class="cm-variable">bits</span>.<span class="cm-variable">qos</span>, <span class="cm-operator">&amp;</span>(<span class="cm-variable">message</span><span class="cm-operator">-&gt;</span><span class="cm-variable">msg</span>.<span class="cm-variable">publish</span>));</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">break</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">case</span> <span class="cm-variable">UMQTT_TYPE_PUBACK</span>:</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">umqtt_puback_encode</span>(<span class="cm-variable">send_buf</span>, <span class="cm-variable">send_len</span>, <span class="cm-variable">message</span><span class="cm-operator">-&gt;</span><span class="cm-variable">msg</span>.<span class="cm-variable">puback</span>.<span class="cm-variable">packet_id</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">break</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">case</span> <span class="cm-variable">UMQTT_TYPE_PUBREC</span>:</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">// _ret = umqtt_pubrec_encode();</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">break</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">case</span> <span class="cm-variable">UMQTT_TYPE_PUBREL</span>:</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">umqtt_pubrel_encode</span>(<span class="cm-variable">send_buf</span>, <span class="cm-variable">send_len</span>, <span class="cm-variable">message</span><span class="cm-operator">-&gt;</span><span class="cm-variable">header</span>.<span class="cm-variable">bits</span>.<span class="cm-variable">dup</span>, <span class="cm-variable">message</span><span class="cm-operator">-&gt;</span><span class="cm-variable">msg</span>.<span class="cm-variable">pubrel</span>.<span class="cm-variable">packet_id</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">break</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">case</span> <span class="cm-variable">UMQTT_TYPE_PUBCOMP</span>:</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">umqtt_pubcomp_encode</span>(<span class="cm-variable">send_buf</span>, <span class="cm-variable">send_len</span>, <span class="cm-variable">message</span><span class="cm-operator">-&gt;</span><span class="cm-variable">msg</span>.<span class="cm-variable">pubcomp</span>.<span class="cm-variable">packet_id</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">break</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">case</span> <span class="cm-variable">UMQTT_TYPE_SUBSCRIBE</span>:</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">umqtt_subscribe_encode</span>(<span class="cm-variable">send_buf</span>, <span class="cm-variable">send_len</span>, <span class="cm-operator">&amp;</span>(<span class="cm-variable">message</span><span class="cm-operator">-&gt;</span><span class="cm-variable">msg</span>.<span class="cm-variable">subscribe</span>));</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">break</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">case</span> <span class="cm-variable">UMQTT_TYPE_UNSUBSCRIBE</span>:</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">umqtt_unsubscribe_encode</span>(<span class="cm-variable">send_buf</span>, <span class="cm-variable">send_len</span>, <span class="cm-operator">&amp;</span>(<span class="cm-variable">message</span><span class="cm-operator">-&gt;</span><span class="cm-variable">msg</span>.<span class="cm-variable">unsubscribe</span>));</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">break</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">case</span> <span class="cm-variable">UMQTT_TYPE_PINGREQ</span>:</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">umqtt_pingreq_encode</span>(<span class="cm-variable">send_buf</span>, <span class="cm-variable">send_len</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">break</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">case</span> <span class="cm-variable">UMQTT_TYPE_DISCONNECT</span>:</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">umqtt_disconnect_encode</span>(<span class="cm-variable">send_buf</span>, <span class="cm-variable">send_len</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">break</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">default</span>:</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">break</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">return</span> <span class="cm-variable">_ret</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">}</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 1219px;"></div><div class="CodeMirror-gutters" style="display: none; height: 1219px;"></div></div></div></pre><p><span>其中，MQTT控制报文类型的宏定义，与</span><a href='#211-mqtt-control-packet-type'><span>2.1.1 MQTT Control Packet type</span></a><span>相对应:</span></p><pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="C" style="break-inside: unset;"><div class="CodeMirror cm-s-inner cm-s-null-scroll CodeMirror-wrap" lang="c"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 9.5px; left: 8px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">enum</span> <span class="cm-variable">umqtt_type</span></span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">{</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">UMQTT_TYPE_RESERVED</span> &nbsp; &nbsp; &nbsp;<span class="cm-operator">=</span> <span class="cm-number">0</span>,</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">UMQTT_TYPE_CONNECT</span> &nbsp; &nbsp; &nbsp; <span class="cm-operator">=</span> <span class="cm-number">1</span>,</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">UMQTT_TYPE_CONNACK</span> &nbsp; &nbsp; &nbsp; <span class="cm-operator">=</span> <span class="cm-number">2</span>,</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">UMQTT_TYPE_PUBLISH</span> &nbsp; &nbsp; &nbsp; <span class="cm-operator">=</span> <span class="cm-number">3</span>,</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">UMQTT_TYPE_PUBACK</span> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-operator">=</span> <span class="cm-number">4</span>,</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">UMQTT_TYPE_PUBREC</span> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-operator">=</span> <span class="cm-number">5</span>,</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">UMQTT_TYPE_PUBREL</span> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-operator">=</span> <span class="cm-number">6</span>,</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">UMQTT_TYPE_PUBCOMP</span> &nbsp; &nbsp; &nbsp; <span class="cm-operator">=</span> <span class="cm-number">7</span>,</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">UMQTT_TYPE_SUBSCRIBE</span> &nbsp; &nbsp; <span class="cm-operator">=</span> <span class="cm-number">8</span>,</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">UMQTT_TYPE_SUBACK</span> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-operator">=</span> <span class="cm-number">9</span>,</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">UMQTT_TYPE_UNSUBSCRIBE</span> &nbsp; <span class="cm-operator">=</span> <span class="cm-number">10</span>,</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">UMQTT_TYPE_UNSUBACK</span> &nbsp; &nbsp; &nbsp;<span class="cm-operator">=</span> <span class="cm-number">11</span>,</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">UMQTT_TYPE_PINGREQ</span> &nbsp; &nbsp; &nbsp; <span class="cm-operator">=</span> <span class="cm-number">12</span>,</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">UMQTT_TYPE_PINGRESP</span> &nbsp; &nbsp; &nbsp;<span class="cm-operator">=</span> <span class="cm-number">13</span>,</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">UMQTT_TYPE_DISCONNECT</span> &nbsp; &nbsp;<span class="cm-operator">=</span> <span class="cm-number">14</span>,</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">};</span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 414px;"></div><div class="CodeMirror-gutters" style="display: none; height: 414px;"></div></div></div></pre><p><span>由于报文类型较多，接下来仅以</span><strong><a href='http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718028'><span>CONNECT</span></a></strong><span>报文（可变header —— “协议名称”、“协议等级”、“连接标志”、“保活间隔(秒)”，有效载荷——“客户端标识符”、“遗嘱主题”、“遗嘱消息”、“用户名”、“密码”）为例，来简述uMQTT的组包过程：</span></p><ol start='' ><li><p><span>填充MQTT客户端的默认配置信息</span></p><pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="C" style="break-inside: unset;"><div class="CodeMirror cm-s-inner cm-s-null-scroll CodeMirror-wrap" lang="c"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 9.5px; left: 8px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">encode_msg</span>.<span class="cm-variable">msg</span>.<span class="cm-variable">connect</span>.<span class="cm-variable">protocol_name_len</span> <span class="cm-operator">=</span> <span class="cm-variable">PKG_UMQTT_PROTOCOL_NAME_LEN</span>;</span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">encode_msg</span>.<span class="cm-variable">msg</span>.<span class="cm-variable">connect</span>.<span class="cm-variable">protocol_name</span> <span class="cm-operator">=</span> <span class="cm-variable">PKG_UMQTT_PROTOCOL_NAME</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">encode_msg</span>.<span class="cm-variable">msg</span>.<span class="cm-variable">connect</span>.<span class="cm-variable">protocol_level</span> <span class="cm-operator">=</span> <span class="cm-variable">PKG_UMQTT_PROTOCOL_LEVEL</span>; <span class="cm-comment">/* MQTT3.1.1 ver_lvl:4;  MQTT3.1 ver_lvl:3 */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">encode_msg</span>.<span class="cm-variable">msg</span>.<span class="cm-variable">connect</span>.<span class="cm-variable">connect_flags</span>.<span class="cm-variable">connect_sign</span> <span class="cm-operator">=</span> <span class="cm-variable">UMQTT_DEF_CONNECT_FLAGS</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-meta">#ifdef PKG_UMQTT_TEST_SHORT_KEEPALIVE_TIME</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">encode_msg</span>.<span class="cm-variable">msg</span>.<span class="cm-variable">connect</span>.<span class="cm-variable">keepalive_interval_sec</span> <span class="cm-operator">=</span> ((<span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">mqtt_info</span>.<span class="cm-variable">connect_keepalive_sec</span> <span class="cm-operator">==</span> <span class="cm-number">0</span>) <span class="cm-operator">?</span> <span class="cm-variable">PKG_UMQTT_CONNECT_KEEPALIVE_DEF_TIME</span> : <span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">mqtt_info</span>.<span class="cm-variable">connect_keepalive_sec</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-meta">#else</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">encode_msg</span>.<span class="cm-variable">msg</span>.<span class="cm-variable">connect</span>.<span class="cm-variable">keepalive_interval_sec</span> <span class="cm-operator">=</span> <span class="cm-variable">PKG_UMQTT_CONNECT_KEEPALIVE_DEF_TIME</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-meta">#endif</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">encode_msg</span>.<span class="cm-variable">msg</span>.<span class="cm-variable">connect</span>.<span class="cm-variable">client_id</span> <span class="cm-operator">=</span> <span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">mqtt_info</span>.<span class="cm-variable">client_id</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">encode_msg</span>.<span class="cm-variable">msg</span>.<span class="cm-variable">connect</span>.<span class="cm-variable">will_topic</span> <span class="cm-operator">=</span> <span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">mqtt_info</span>.<span class="cm-variable">lwt_topic</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">encode_msg</span>.<span class="cm-variable">msg</span>.<span class="cm-variable">connect</span>.<span class="cm-variable">will_message</span> <span class="cm-operator">=</span> <span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">mqtt_info</span>.<span class="cm-variable">lwt_message</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">encode_msg</span>.<span class="cm-variable">msg</span>.<span class="cm-variable">connect</span>.<span class="cm-variable">user_name</span> <span class="cm-operator">=</span> <span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">mqtt_info</span>.<span class="cm-variable">user_name</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">if</span> (<span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">mqtt_info</span>.<span class="cm-variable">user_name</span>) </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">encode_msg</span>.<span class="cm-variable">msg</span>.<span class="cm-variable">connect</span>.<span class="cm-variable">connect_flags</span>.<span class="cm-variable">bits</span>.<span class="cm-variable">username_flag</span> <span class="cm-operator">=</span> <span class="cm-number">1</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">encode_msg</span>.<span class="cm-variable">msg</span>.<span class="cm-variable">connect</span>.<span class="cm-variable">password</span> <span class="cm-operator">=</span> <span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">mqtt_info</span>.<span class="cm-variable">password</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">if</span> (<span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">mqtt_info</span>.<span class="cm-variable">password</span>) {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">encode_msg</span>.<span class="cm-variable">msg</span>.<span class="cm-variable">connect</span>.<span class="cm-variable">connect_flags</span>.<span class="cm-variable">bits</span>.<span class="cm-variable">password_flag</span> <span class="cm-operator">=</span> <span class="cm-number">1</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">encode_msg</span>.<span class="cm-variable">msg</span>.<span class="cm-variable">connect</span>.<span class="cm-variable">password_len</span> <span class="cm-operator">=</span> <span class="cm-variable">rt_strlen</span>(<span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">mqtt_info</span>.<span class="cm-variable">password</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 529px;"></div><div class="CodeMirror-gutters" style="display: none; height: 529px;"></div></div></div></pre></li><li><p><span>调用</span><code>umqtt_encode</code><span>→</span><code>umqtt_connect_encode</code><span>编码函数（仅封装了</span><code>MQTTSerialize_connect</code><span>）组包：</span></p><pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="C" style="break-inside: unset;"><div class="CodeMirror cm-s-inner cm-s-null-scroll CodeMirror-wrap" lang="c"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 9.5px; left: 8px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">static</span> <span class="cm-variable-3">int</span> <span class="cm-def">MQTTSerialize_connect</span>(<span class="cm-variable-3">unsigned</span> <span class="cm-variable-3">char*</span> <span class="cm-variable">buf</span>, <span class="cm-variable-3">int</span> <span class="cm-variable">buflen</span>, <span class="cm-variable">MQTTPacket_connectData</span><span class="cm-operator">*</span> <span class="cm-variable">options</span>)</span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">{</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-variable-3">unsigned</span> <span class="cm-variable-3">char</span> <span class="cm-variable-3">*</span><span class="cm-variable">ptr</span> <span class="cm-operator">=</span> <span class="cm-variable">buf</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-variable">MQTTHeader</span> <span class="cm-variable">header</span> <span class="cm-operator">=</span> { <span class="cm-number">0</span> };</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-variable-3">int</span> <span class="cm-variable">len</span> <span class="cm-operator">=</span> <span class="cm-number">0</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-variable-3">int</span> <span class="cm-variable">rc</span> <span class="cm-operator">=</span> <span class="cm-operator">-</span><span class="cm-number">1</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-keyword">if</span> (<span class="cm-variable">umqtt_pkgs_len</span>(<span class="cm-variable">len</span> <span class="cm-operator">=</span> <span class="cm-variable">MQTTSerialize_connectLength</span>(<span class="cm-variable">options</span>)) <span class="cm-operator">&gt;</span> <span class="cm-variable">buflen</span>) </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-variable">rc</span> <span class="cm-operator">=</span> <span class="cm-variable">UMQTT_BUFFER_TOO_SHORT</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-keyword">goto</span> <span class="cm-variable">exit</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span>}</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-variable">header</span>.<span class="cm-variable">byte</span> <span class="cm-operator">=</span> <span class="cm-number">0</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-variable">header</span>.<span class="cm-variable">bits</span>.<span class="cm-variable">type</span> <span class="cm-operator">=</span> <span class="cm-variable">UMQTT_TYPE_CONNECT</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-variable">umqtt_writeChar</span>(<span class="cm-operator">&amp;</span><span class="cm-variable">ptr</span>, <span class="cm-variable">header</span>.<span class="cm-variable">byte</span>); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* 写固定header的第一个字节 */</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-variable">ptr</span> <span class="cm-operator">+=</span> <span class="cm-variable">umqtt_pkgs_encode</span>(<span class="cm-variable">ptr</span>, <span class="cm-variable">len</span>); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* 写剩余长度 */</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-keyword">if</span> (<span class="cm-variable">options</span><span class="cm-operator">-&gt;</span><span class="cm-variable">protocol_level</span> <span class="cm-operator">==</span> <span class="cm-number">4</span>) <span class="cm-tab" role="presentation" cm-text="	">  </span><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-comment">/* MQTT V3.1.1 */</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-variable">umqtt_writeCString</span>(<span class="cm-operator">&amp;</span><span class="cm-variable">ptr</span>, <span class="cm-string">"MQTT"</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-variable">umqtt_writeChar</span>(<span class="cm-operator">&amp;</span><span class="cm-variable">ptr</span>, (<span class="cm-variable-3">char</span>) <span class="cm-number">4</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span>} </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">else</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-variable">umqtt_writeCString</span>(<span class="cm-operator">&amp;</span><span class="cm-variable">ptr</span>, <span class="cm-string">"MQIsdp"</span>);<span class="cm-tab" role="presentation" cm-text="	"> </span><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-comment">/* MQTT V3.1 */</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-variable">umqtt_writeChar</span>(<span class="cm-operator">&amp;</span><span class="cm-variable">ptr</span>, (<span class="cm-variable-3">char</span>) <span class="cm-number">3</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span>}</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-variable">umqtt_writeChar</span>(<span class="cm-operator">&amp;</span><span class="cm-variable">ptr</span>, <span class="cm-variable">options</span><span class="cm-operator">-&gt;</span><span class="cm-variable">connect_flags</span>.<span class="cm-variable">connect_sign</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-variable">umqtt_writeInt</span>(<span class="cm-operator">&amp;</span><span class="cm-variable">ptr</span>, <span class="cm-variable">options</span><span class="cm-operator">-&gt;</span><span class="cm-variable">keepalive_interval_sec</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-comment">// umqtt_writeInt(&amp;ptr, PKG_UMQTT_CONNECT_KEEPALIVE_DEF_TIME); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* ping interval max, 0xffff */ </span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-variable">umqtt_writeMQTTString</span>(<span class="cm-operator">&amp;</span><span class="cm-variable">ptr</span>, <span class="cm-variable">options</span><span class="cm-operator">-&gt;</span><span class="cm-variable">client_id</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-keyword">if</span> (<span class="cm-variable">options</span><span class="cm-operator">-&gt;</span><span class="cm-variable">connect_flags</span>.<span class="cm-variable">bits</span>.<span class="cm-variable">will_flag</span>) </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-variable">umqtt_writeMQTTString</span>(<span class="cm-operator">&amp;</span><span class="cm-variable">ptr</span>, <span class="cm-variable">options</span><span class="cm-operator">-&gt;</span><span class="cm-variable">will_topic</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-variable">umqtt_writeMQTTString</span>(<span class="cm-operator">&amp;</span><span class="cm-variable">ptr</span>, <span class="cm-variable">options</span><span class="cm-operator">-&gt;</span><span class="cm-variable">will_message</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span>}</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-keyword">if</span> (<span class="cm-variable">options</span><span class="cm-operator">-&gt;</span><span class="cm-variable">connect_flags</span>.<span class="cm-variable">bits</span>.<span class="cm-variable">username_flag</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-variable">umqtt_writeMQTTString</span>(<span class="cm-operator">&amp;</span><span class="cm-variable">ptr</span>, <span class="cm-variable">options</span><span class="cm-operator">-&gt;</span><span class="cm-variable">user_name</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-keyword">if</span> (<span class="cm-variable">options</span><span class="cm-operator">-&gt;</span><span class="cm-variable">connect_flags</span>.<span class="cm-variable">bits</span>.<span class="cm-variable">password_flag</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-variable">umqtt_writeMQTTString</span>(<span class="cm-operator">&amp;</span><span class="cm-variable">ptr</span>, <span class="cm-variable">options</span><span class="cm-operator">-&gt;</span><span class="cm-variable">password</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-variable">rc</span> <span class="cm-operator">=</span> <span class="cm-variable">ptr</span> <span class="cm-operator">-</span> <span class="cm-variable">buf</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">exit</span>:</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-keyword">return</span> <span class="cm-variable">rc</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 1173px;"></div><div class="CodeMirror-gutters" style="display: none; height: 1173px;"></div></div></div></pre><p><span>该函数首先调用</span><code>MQTTSerialize_connectLength</code><span>来计算</span><strong><span>可变header</span></strong><span>和</span><strong><span>有效载荷</span></strong><span>的长度，得到的len会被作为参数传递给</span><code>umqtt_pkgs_len</code><span>函数，它的作用是计算</span><strong><span>固定header</span></strong><span>中的</span><code>剩余长度</code><span>字段的字节数并加上</span><strong><span>固定header</span></strong><span>第一个字节长度即1，与buflen作比较，判断该包数据的有效性。</span></p><blockquote><p><span>为什么这里使用了</span><code>if (umqtt_pkgs_len(len = MQTTSerialize_connectLength(options)) &gt; buflen)</code><span>这种的复合语句呢？</span></p><p><span>因为我们希望得到的len长度就是</span><strong><span>固定header</span></strong><span>中的</span><code>剩余长度</code><span>值从而方便后面的组包过程，而有效的报文长度buflen = len + 1 + </span><code>剩余长度</code><span>字段的字节数；如果直接计算报文长度，当后面写入</span><code>剩余长度</code><span>值时，还需要减去自身字节长以及</span><strong><span>固定header</span></strong><span>第一个字节长度即1，更加复杂繁琐。</span></p></blockquote><p><span>这里拉出几个重要的结构体和共用体，来和MQTT协议对应一下：</span></p><ul><li><p><span>固定header</span></p><p><span>可参考</span><a href='#21-fixed-header'><strong><span>2.1. Fixed header</span></strong></a></p><pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="c"><div class="CodeMirror cm-s-inner cm-s-null-scroll CodeMirror-wrap" lang="c"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 9.5px; left: 8px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">union</span> <span class="cm-variable">umqtt_pkgs_fix_header</span></span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">{</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">rt_uint8_t</span> <span class="cm-variable">byte</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">/* header */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">struct</span> { &nbsp; &nbsp; &nbsp; &nbsp;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">rt_uint8_t</span> <span class="cm-variable">retain</span>: <span class="cm-number">1</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* reserved bits */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">rt_uint8_t</span> <span class="cm-variable">qos</span>: &nbsp; &nbsp;<span class="cm-number">2</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* QoS, 0-Almost once; 1-Alteast once; 2-Exactly once */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">rt_uint8_t</span> <span class="cm-variable">dup</span>: &nbsp; &nbsp;<span class="cm-number">1</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* dup flag */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">rt_uint8_t</span> <span class="cm-variable">type</span>: &nbsp; <span class="cm-number">4</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* MQTT packet type */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  } <span class="cm-variable">bits</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">};</span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 230px;"></div><div class="CodeMirror-gutters" style="display: none; height: 230px;"></div></div></div></pre></li><li><p><span>CONNECT标志位</span></p><p><span>各标志位的作用，此处不再赘述，可参考</span><a href='https://mcxiaoke.gitbook.io/mqtt/03-controlpackets/0301-connect#lian-jie-biao-zhi-connect-flags'><span>这里</span></a><span>。</span></p><table>
    <tbody><tr>
        <td align="center"><b>Bit</b></td>
        <td align="center"><b>7</b></td>
        <td align="center"><b>6</b></td>
        <td align="center"><b>5</b></td>
        <td align="center"><b>4</b></td>
        <td align="center"><b>3</b></td>
        <td align="center"><b>2</b></td>
        <td align="center"><b>1</b></td>
        <td align="center"><b>0</b></td>
    </tr>
    <tr>
        <td>描述</td>
        <td align="center">User Name Flag</td>
        <td align="center">Password Flag</td>
        <td align="center">Will Retain</td>
        <td colspan="2" ,="" align="center">Will QoS</td>
        <td align="center">Will Flag</td>
        <td align="center">Clean Session</td>
        <td align="center">Reserved</td>
    </tr>
    <tr>
        <td>byte 8</td>
        <td align="center">X</td>
        <td align="center">X</td>
        <td align="center">X</td>
        <td align="center">X</td>
        <td align="center">X</td>
        <td align="center">X</td>
        <td align="center">X</td>
        <td align="center">0</td>
</tr></tbody></table><pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="c" style="break-inside: unset;"><div class="CodeMirror cm-s-inner cm-s-null-scroll CodeMirror-wrap" lang="c"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 9.5px; left: 8px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">union</span> <span class="cm-variable">umqtt_pkgs_connect_sign</span></span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">{</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">rt_uint8_t</span> <span class="cm-variable">connect_sign</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">struct</span> {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">rt_uint8_t</span> <span class="cm-variable">reserved</span>: &nbsp; &nbsp; &nbsp; <span class="cm-number">1</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* reserved bits */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">rt_uint8_t</span> <span class="cm-variable">clean_session</span>: &nbsp;<span class="cm-number">1</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* clean session bit */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">rt_uint8_t</span> <span class="cm-variable">will_flag</span>: &nbsp; &nbsp; &nbsp;<span class="cm-number">1</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* will flag bit */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">rt_uint8_t</span> <span class="cm-variable">will_Qos</span>: &nbsp; &nbsp; &nbsp; <span class="cm-number">2</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* will Qos bit */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">rt_uint8_t</span> <span class="cm-variable">will_retain</span>: &nbsp; &nbsp;<span class="cm-number">1</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* will retain bit */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">rt_uint8_t</span> <span class="cm-variable">password_flag</span>: &nbsp;<span class="cm-number">1</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* password flag bit */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">rt_uint8_t</span> <span class="cm-variable">username_flag</span>: &nbsp;<span class="cm-number">1</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* user name flag bit */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  } <span class="cm-variable">bits</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">};</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-operator">-------------------------------------------------------------------------------</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-meta">#define UMQTT_SET_CONNECT_FLAGS(user_name_flag, password_flag, will_retain, will_qos, will_flag, clean_session, reserved) &nbsp;  \</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-meta">(((user_name_flag &amp; 0x01) &lt;&lt; 7) | &nbsp;  \</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-meta">((password_flag &amp; 0x01) &lt;&lt; 6) |  \</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-meta">((will_retain &amp; 0x01) &lt;&lt; 5) | &nbsp; \</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-meta">((will_qos &amp; 0x01) &lt;&lt; 3) | &nbsp; \</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-meta">((will_flag &amp; 0x01) &lt;&lt; 2) | \</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-meta">((clean_session &amp; 0x01) &lt;&lt; 1) | \</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-meta">(reserved &amp; 0x01))</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-meta">#define UMQTT_DEF_CONNECT_FLAGS &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (UMQTT_SET_CONNECT_FLAGS(0,0,0,0,0,1,0))</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 552px;"></div><div class="CodeMirror-gutters" style="display: none; height: 552px;"></div></div></div></pre></li></ul></li></ol><p><span>以上组包过程完成之后，会调用</span><code>umqtt_trans_send</code><span>函数，通过LWIP将发送缓冲区数据发送到socket连接的Broker：</span></p><pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="c" style="break-inside: unset;"><div class="CodeMirror cm-s-inner cm-s-null-scroll CodeMirror-wrap" lang="c"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 9.5px; left: 8px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment">/** </span></span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* TCP/TLS send datas on configured transport.</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">*</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* @param sock the input socket</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* @param send_buf the input， transport datas buffer</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* @param buf_len the input, transport datas buffer length</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* @param timeout the input, tcp/tls transport timeout</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">*</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* @return &lt;0: failed or other error</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* &nbsp; &nbsp; &nbsp; &nbsp; =0: success</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">*/</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable-3">int</span> <span class="cm-def">umqtt_trans_send</span>(<span class="cm-variable-3">int</span> <span class="cm-variable">sock</span>, <span class="cm-keyword">const</span> <span class="cm-variable">rt_uint8_t</span> <span class="cm-operator">*</span><span class="cm-variable">send_buf</span>, <span class="cm-variable">rt_uint32_t</span> <span class="cm-variable">buf_len</span>, <span class="cm-variable-3">int</span> <span class="cm-variable">timeout</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">{</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable-3">int</span> <span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-number">0</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">rt_uint32_t</span> <span class="cm-variable">offset</span> <span class="cm-operator">=</span> <span class="cm-number">0U</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">while</span> (<span class="cm-variable">offset</span> <span class="cm-operator">&lt;</span> <span class="cm-variable">buf_len</span>) </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">send</span>(<span class="cm-variable">sock</span>, <span class="cm-variable">send_buf</span> <span class="cm-operator">+</span> <span class="cm-variable">offset</span>, <span class="cm-variable">buf_len</span> <span class="cm-operator">-</span> <span class="cm-variable">offset</span>, <span class="cm-number">0</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">if</span> (<span class="cm-variable">_ret</span> <span class="cm-operator">&lt;</span> <span class="cm-number">0</span>) </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">return</span> <span class="cm-operator">-</span><span class="cm-variable">errno</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">offset</span> <span class="cm-operator">+=</span> <span class="cm-variable">_ret</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">return</span> <span class="cm-variable">_ret</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 575px;"></div><div class="CodeMirror-gutters" style="display: none; height: 575px;"></div></div></div></pre><h3 id='45-umqtt接收解包'><strong><span>4.5. uMQTT接收解包</span></strong></h3><p><span>当uMQTT将CONNECT报文发送完成后，就会调用</span><code>umqtt_handle_readpacket</code><span>函数（完成CONNECT过程后，该函数也会在</span><code>umqtt_thread</code><span>线程中被循环调用来收发数据）读取Broker的回复，对接收到的数据进行解包处理：</span></p><pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="C" style="break-inside: unset;"><div class="CodeMirror cm-s-inner cm-s-null-scroll CodeMirror-wrap" lang="c"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 9.5px; left: 8px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">static</span> <span class="cm-variable-3">int</span> <span class="cm-def">umqtt_handle_readpacket</span>(<span class="cm-keyword">struct</span> <span class="cm-def">umqtt_client</span> <span class="cm-operator">*</span><span class="cm-variable">client</span>)</span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">{</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable-3">int</span> <span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-number">0</span>, <span class="cm-variable">_onedata</span> <span class="cm-operator">=</span> <span class="cm-number">0</span>, <span class="cm-variable">_cnt</span> <span class="cm-operator">=</span> <span class="cm-number">0</span>, <span class="cm-variable">_loop_cnt</span> <span class="cm-operator">=</span> <span class="cm-number">0</span>, <span class="cm-variable">_remain_len</span> <span class="cm-operator">=</span> <span class="cm-number">0</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable-3">int</span> <span class="cm-variable">_temp_ret</span> <span class="cm-operator">=</span> <span class="cm-number">0</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable-3">int</span> <span class="cm-variable">_pkt_len</span> <span class="cm-operator">=</span> <span class="cm-number">0</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable-3">int</span> <span class="cm-variable">_multiplier</span> <span class="cm-operator">=</span> <span class="cm-number">1</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable-3">int</span> <span class="cm-variable">_pkt_type</span> <span class="cm-operator">=</span> <span class="cm-number">0</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">struct</span> <span class="cm-def">umqtt_msg</span> <span class="cm-def">decode_msg</span> <span class="cm-operator">=</span> { <span class="cm-number">0</span> };</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">struct</span> <span class="cm-def">umqtt_msg_ack</span> <span class="cm-def">msg_ack</span> <span class="cm-operator">=</span> { <span class="cm-number">0</span> };</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">struct</span> <span class="cm-def">umqtt_msg</span> <span class="cm-def">encode_msg</span> <span class="cm-operator">=</span> { <span class="cm-number">0</span> };</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">RT_ASSERT</span>(<span class="cm-variable">client</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-comment">/* 1. 读Fixed header的第一个字节 */</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">_temp_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">umqtt_trans_recv</span>(<span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">sock</span>, <span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">recv_buf</span>, <span class="cm-number">1</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">if</span> (<span class="cm-variable">_temp_ret</span> <span class="cm-operator">&lt;=</span> <span class="cm-number">0</span>) </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">UMQTT_FIN_ACK</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">LOG_W</span>(<span class="cm-string">" server fin ack! connect failed! need to reconnect!"</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">goto</span> <span class="cm-variable">exit</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  } </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-comment">/* 2. 读Fixed header的Remaining length字段并解析剩余长度 */</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">do</span> {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">if</span> (<span class="cm-operator">++</span><span class="cm-variable">_cnt</span> <span class="cm-operator">&gt;</span> <span class="cm-variable">MAX_NO_OF_REMAINING_LENGTH_BYTES</span>) </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">UMQTT_FAILED</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">LOG_E</span>(<span class="cm-string">" umqtt packet length error!"</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">goto</span> <span class="cm-variable">exit</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">umqtt_readpacket</span>(<span class="cm-variable">client</span>, (<span class="cm-variable-3">unsigned</span> <span class="cm-variable-3">char</span> <span class="cm-variable-3">*</span>)<span class="cm-operator">&amp;</span><span class="cm-variable">_onedata</span>, <span class="cm-number">1</span>, <span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">mqtt_info</span>.<span class="cm-variable">recv_time_ms</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">if</span> (<span class="cm-variable">_ret</span> <span class="cm-operator">==</span> <span class="cm-variable">UMQTT_FIN_ACK</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">LOG_W</span>(<span class="cm-string">" server fin ack! connect failed! need to reconnect!"</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">goto</span> <span class="cm-variable">exit</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">else</span> <span class="cm-keyword">if</span> (<span class="cm-variable">_ret</span> <span class="cm-operator">!=</span> <span class="cm-variable">UMQTT_OK</span>) </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">UMQTT_READ_FAILED</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">goto</span> <span class="cm-variable">exit</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-operator">*</span>(<span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">recv_buf</span> <span class="cm-operator">+</span> <span class="cm-variable">_cnt</span>) <span class="cm-operator">=</span> <span class="cm-variable">_onedata</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_pkt_len</span> <span class="cm-operator">+=</span> (<span class="cm-variable">_onedata</span> <span class="cm-operator">&amp;</span> <span class="cm-number">0x7F</span>) <span class="cm-operator">*</span> <span class="cm-variable">_multiplier</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_multiplier</span> <span class="cm-operator">*=</span> <span class="cm-number">0x80</span>; </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  } <span class="cm-keyword">while</span> ((<span class="cm-variable">_onedata</span> <span class="cm-operator">&amp;</span> <span class="cm-number">0x80</span>) <span class="cm-operator">!=</span> <span class="cm-number">0</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-comment">/* 异常处理：如果当前报文的数据长度大于缓冲区长度，会将socket缓冲中的数据全部读出来丢掉，返回UMQTT_BUFFER_TOO_SHORT错误 */</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">if</span> ((<span class="cm-variable">_pkt_len</span> <span class="cm-operator">+</span> <span class="cm-number">1</span> <span class="cm-operator">+</span> <span class="cm-variable">_cnt</span>) <span class="cm-operator">&gt;</span> <span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">mqtt_info</span>.<span class="cm-variable">recv_size</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">LOG_W</span>(<span class="cm-string">" socket read buffer too short! will read and delete socket buff! "</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_loop_cnt</span> <span class="cm-operator">=</span> <span class="cm-variable">_pkt_len</span> <span class="cm-operator">/</span> <span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">mqtt_info</span>.<span class="cm-variable">recv_size</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">do</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">if</span> (<span class="cm-variable">_loop_cnt</span> <span class="cm-operator">==</span> <span class="cm-number">0</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">umqtt_readpacket</span>(<span class="cm-variable">client</span>, <span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">recv_buf</span>, <span class="cm-variable">_pkt_len</span>, <span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">mqtt_info</span>.<span class="cm-variable">recv_time_ms</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">UMQTT_BUFFER_TOO_SHORT</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">LOG_W</span>(<span class="cm-string">" finish read and delete socket buff!"</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">goto</span> <span class="cm-variable">exit</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">else</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_loop_cnt</span><span class="cm-operator">--</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">umqtt_readpacket</span>(<span class="cm-variable">client</span>, <span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">recv_buf</span>, <span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">mqtt_info</span>.<span class="cm-variable">recv_size</span>, <span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">mqtt_info</span>.<span class="cm-variable">recv_time_ms</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_pkt_len</span> <span class="cm-operator">-=</span> <span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">mqtt_info</span>.<span class="cm-variable">recv_size</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp;  }<span class="cm-keyword">while</span>(<span class="cm-number">1</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-comment">/* 3. 读剩余数据——可变header+有效载荷 */</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">umqtt_readpacket</span>(<span class="cm-variable">client</span>, <span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">recv_buf</span> <span class="cm-operator">+</span> <span class="cm-variable">_cnt</span> <span class="cm-operator">+</span> <span class="cm-number">1</span>, <span class="cm-variable">_pkt_len</span>, <span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">mqtt_info</span>.<span class="cm-variable">recv_time_ms</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">if</span> (<span class="cm-variable">_ret</span> <span class="cm-operator">==</span> <span class="cm-variable">UMQTT_FIN_ACK</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">LOG_W</span>(<span class="cm-string">" server fin ack! connect failed! need to reconnect!"</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">goto</span> <span class="cm-variable">exit</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">else</span> <span class="cm-keyword">if</span> (<span class="cm-variable">_ret</span> <span class="cm-operator">!=</span> <span class="cm-variable">UMQTT_OK</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">UMQTT_READ_FAILED</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">LOG_E</span>(<span class="cm-string">" read remain datas error!"</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">goto</span> <span class="cm-variable">exit</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-comment">/* 4. 解析数据包，并根据不同报文类型做相应处理 */</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">rt_memset</span>(<span class="cm-operator">&amp;</span><span class="cm-variable">decode_msg</span>, <span class="cm-number">0</span>, <span class="cm-keyword">sizeof</span>(<span class="cm-variable">decode_msg</span>));</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">umqtt_decode</span>(<span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">recv_buf</span>, <span class="cm-variable">_pkt_len</span> <span class="cm-operator">+</span> <span class="cm-variable">_cnt</span> <span class="cm-operator">+</span> <span class="cm-number">1</span>, <span class="cm-operator">&amp;</span><span class="cm-variable">decode_msg</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">if</span> (<span class="cm-variable">_ret</span> <span class="cm-operator">&lt;</span> <span class="cm-number">0</span>) </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">UMQTT_DECODE_ERROR</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">LOG_E</span>(<span class="cm-string">" decode error!"</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">goto</span> <span class="cm-variable">exit</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">_pkt_type</span> <span class="cm-operator">=</span> <span class="cm-variable">decode_msg</span>.<span class="cm-variable">header</span>.<span class="cm-variable">bits</span>.<span class="cm-variable">type</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">switch</span> (<span class="cm-variable">_pkt_type</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">case</span> <span class="cm-variable">UMQTT_TYPE_CONNACK</span>:</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">LOG_D</span>(<span class="cm-string">" read connack cmd information!"</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">set_uplink_recon_tick</span>(<span class="cm-variable">client</span>, <span class="cm-variable">UPLINK_NEXT_TICK</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">set_connect_status</span>(<span class="cm-variable">client</span>, <span class="cm-variable">UMQTT_CS_LINKED</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">break</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">case</span> <span class="cm-variable">UMQTT_TYPE_PUBLISH</span>:</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">LOG_D</span>(<span class="cm-string">" read publish cmd information!"</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">set_uplink_recon_tick</span>(<span class="cm-variable">client</span>, <span class="cm-variable">UPLINK_NEXT_TICK</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">if</span> (<span class="cm-variable">decode_msg</span>.<span class="cm-variable">header</span>.<span class="cm-variable">bits</span>.<span class="cm-variable">qos</span> <span class="cm-operator">!=</span> <span class="cm-variable">UMQTT_QOS2</span>) </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">LOG_D</span>(<span class="cm-string">" qos: %d, deliver message! topic nme: %s "</span>, <span class="cm-variable">decode_msg</span>.<span class="cm-variable">header</span>.<span class="cm-variable">bits</span>.<span class="cm-variable">qos</span>, <span class="cm-variable">decode_msg</span>.<span class="cm-variable">msg</span>.<span class="cm-variable">publish</span>.<span class="cm-variable">topic_name</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">umqtt_deliver_message</span>(<span class="cm-variable">client</span>, <span class="cm-variable">decode_msg</span>.<span class="cm-variable">msg</span>.<span class="cm-variable">publish</span>.<span class="cm-variable">topic_name</span>, <span class="cm-variable">decode_msg</span>.<span class="cm-variable">msg</span>.<span class="cm-variable">publish</span>.<span class="cm-variable">topic_name_len</span>, </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-operator">&amp;</span>(<span class="cm-variable">decode_msg</span>.<span class="cm-variable">msg</span>.<span class="cm-variable">publish</span>));</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">if</span> (<span class="cm-variable">decode_msg</span>.<span class="cm-variable">header</span>.<span class="cm-variable">bits</span>.<span class="cm-variable">qos</span> <span class="cm-operator">!=</span> <span class="cm-variable">UMQTT_QOS0</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  { &nbsp; </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">rt_memset</span>(<span class="cm-operator">&amp;</span><span class="cm-variable">encode_msg</span>, <span class="cm-number">0</span>, <span class="cm-keyword">sizeof</span>(<span class="cm-variable">encode_msg</span>));</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">encode_msg</span>.<span class="cm-variable">header</span>.<span class="cm-variable">bits</span>.<span class="cm-variable">qos</span> <span class="cm-operator">=</span> <span class="cm-variable">decode_msg</span>.<span class="cm-variable">header</span>.<span class="cm-variable">bits</span>.<span class="cm-variable">qos</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">encode_msg</span>.<span class="cm-variable">header</span>.<span class="cm-variable">bits</span>.<span class="cm-variable">dup</span> <span class="cm-operator">=</span> <span class="cm-variable">decode_msg</span>.<span class="cm-variable">header</span>.<span class="cm-variable">bits</span>.<span class="cm-variable">dup</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">if</span> (<span class="cm-variable">decode_msg</span>.<span class="cm-variable">header</span>.<span class="cm-variable">bits</span>.<span class="cm-variable">qos</span> <span class="cm-operator">==</span> <span class="cm-variable">UMQTT_QOS1</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">encode_msg</span>.<span class="cm-variable">header</span>.<span class="cm-variable">bits</span>.<span class="cm-variable">type</span> <span class="cm-operator">=</span> <span class="cm-variable">UMQTT_TYPE_PUBACK</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">encode_msg</span>.<span class="cm-variable">msg</span>.<span class="cm-variable">puback</span>.<span class="cm-variable">packet_id</span> <span class="cm-operator">=</span> <span class="cm-variable">decode_msg</span>.<span class="cm-variable">msg</span>.<span class="cm-variable">publish</span>.<span class="cm-variable">packet_id</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">else</span> <span class="cm-keyword">if</span> (<span class="cm-variable">decode_msg</span>.<span class="cm-variable">header</span>.<span class="cm-variable">bits</span>.<span class="cm-variable">qos</span> <span class="cm-operator">==</span> <span class="cm-variable">UMQTT_QOS2</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">encode_msg</span>.<span class="cm-variable">header</span>.<span class="cm-variable">bits</span>.<span class="cm-variable">type</span> <span class="cm-operator">=</span> <span class="cm-variable">UMQTT_TYPE_PUBREC</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">add_one_qos2_msg</span>(<span class="cm-variable">client</span>, <span class="cm-operator">&amp;</span>(<span class="cm-variable">decode_msg</span>.<span class="cm-variable">msg</span>.<span class="cm-variable">publish</span>));</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">encode_msg</span>.<span class="cm-variable">msg</span>.<span class="cm-variable">pubrel</span>.<span class="cm-variable">packet_id</span> <span class="cm-operator">=</span> <span class="cm-variable">decode_msg</span>.<span class="cm-variable">msg</span>.<span class="cm-variable">publish</span>.<span class="cm-variable">packet_id</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">add_one_pubrec_msg</span>(<span class="cm-variable">client</span>, <span class="cm-variable">encode_msg</span>.<span class="cm-variable">msg</span>.<span class="cm-variable">pubrel</span>.<span class="cm-variable">packet_id</span>); &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">/* add pubrec message */</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">umqtt_encode</span>(<span class="cm-variable">encode_msg</span>.<span class="cm-variable">header</span>.<span class="cm-variable">bits</span>.<span class="cm-variable">type</span>, <span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">send_buf</span>, <span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">mqtt_info</span>.<span class="cm-variable">send_size</span>, </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-operator">&amp;</span><span class="cm-variable">encode_msg</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">if</span> (<span class="cm-variable">_ret</span> <span class="cm-operator">&lt;</span> <span class="cm-number">0</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">UMQTT_ENCODE_ERROR</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">LOG_E</span>(<span class="cm-string">" puback / pubrec failed!"</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">goto</span> <span class="cm-variable">exit</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">send_len</span> <span class="cm-operator">=</span> <span class="cm-variable">_ret</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">umqtt_trans_send</span>(<span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">sock</span>, <span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">send_buf</span>, <span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">send_len</span>, </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">mqtt_info</span>.<span class="cm-variable">send_timeout</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">if</span> (<span class="cm-variable">_ret</span> <span class="cm-operator">&lt;</span> <span class="cm-number">0</span>) </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">UMQTT_SEND_FAILED</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">LOG_E</span>(<span class="cm-string">" trans send failed!"</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">goto</span> <span class="cm-variable">exit</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">break</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">case</span> <span class="cm-variable">UMQTT_TYPE_PUBACK</span>:</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">LOG_D</span>(<span class="cm-string">" read puback cmd information!"</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">rt_memset</span>(<span class="cm-operator">&amp;</span><span class="cm-variable">msg_ack</span>, <span class="cm-number">0</span>, <span class="cm-keyword">sizeof</span>(<span class="cm-variable">msg_ack</span>));</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">msg_ack</span>.<span class="cm-variable">msg_type</span> <span class="cm-operator">=</span> <span class="cm-variable">UMQTT_TYPE_PUBACK</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">msg_ack</span>.<span class="cm-variable">packet_id</span> <span class="cm-operator">=</span> <span class="cm-variable">decode_msg</span>.<span class="cm-variable">msg</span>.<span class="cm-variable">puback</span>.<span class="cm-variable">packet_id</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">rt_mq_send</span>(<span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">msg_queue</span>, <span class="cm-operator">&amp;</span><span class="cm-variable">msg_ack</span>, <span class="cm-keyword">sizeof</span>(<span class="cm-keyword">struct</span> <span class="cm-def">umqtt_msg_ack</span>));</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">if</span> (<span class="cm-variable">_ret</span> <span class="cm-operator">!=</span> <span class="cm-variable">RT_EOK</span>) </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">UMQTT_SEND_FAILED</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">LOG_E</span>(<span class="cm-string">" mq send failed!"</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">goto</span> <span class="cm-variable">exit</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">set_uplink_recon_tick</span>(<span class="cm-variable">client</span>, <span class="cm-variable">UPLINK_NEXT_TICK</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">break</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">case</span> <span class="cm-variable">UMQTT_TYPE_PUBREC</span>:</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">LOG_D</span>(<span class="cm-string">" read pubrec cmd information!"</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">rt_memset</span>(<span class="cm-operator">&amp;</span><span class="cm-variable">msg_ack</span>, <span class="cm-number">0</span>, <span class="cm-keyword">sizeof</span>(<span class="cm-variable">msg_ack</span>));</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">msg_ack</span>.<span class="cm-variable">msg_type</span> <span class="cm-operator">=</span> <span class="cm-variable">UMQTT_TYPE_PUBREC</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">msg_ack</span>.<span class="cm-variable">packet_id</span> <span class="cm-operator">=</span> <span class="cm-variable">decode_msg</span>.<span class="cm-variable">msg</span>.<span class="cm-variable">puback</span>.<span class="cm-variable">packet_id</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">rt_mq_send</span>(<span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">msg_queue</span>, <span class="cm-operator">&amp;</span><span class="cm-variable">msg_ack</span>, <span class="cm-keyword">sizeof</span>(<span class="cm-keyword">struct</span> <span class="cm-def">umqtt_msg_ack</span>));</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">if</span> (<span class="cm-variable">_ret</span> <span class="cm-operator">!=</span> <span class="cm-variable">RT_EOK</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">UMQTT_SEND_FAILED</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">LOG_E</span>(<span class="cm-string">" mq send failed!"</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">goto</span> <span class="cm-variable">exit</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">set_uplink_recon_tick</span>(<span class="cm-variable">client</span>, <span class="cm-variable">UPLINK_NEXT_TICK</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">break</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">case</span> <span class="cm-variable">UMQTT_TYPE_PUBREL</span>:</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">LOG_D</span>(<span class="cm-string">" read pubrel cmd information!"</span>); &nbsp; </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">rt_memset</span>(<span class="cm-operator">&amp;</span><span class="cm-variable">encode_msg</span>, <span class="cm-number">0</span>, <span class="cm-keyword">sizeof</span>(<span class="cm-variable">encode_msg</span>));</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">encode_msg</span>.<span class="cm-variable">header</span>.<span class="cm-variable">bits</span>.<span class="cm-variable">type</span> <span class="cm-operator">=</span> <span class="cm-variable">UMQTT_TYPE_PUBCOMP</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">encode_msg</span>.<span class="cm-variable">header</span>.<span class="cm-variable">bits</span>.<span class="cm-variable">qos</span> <span class="cm-operator">=</span> <span class="cm-variable">decode_msg</span>.<span class="cm-variable">header</span>.<span class="cm-variable">bits</span>.<span class="cm-variable">qos</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">encode_msg</span>.<span class="cm-variable">header</span>.<span class="cm-variable">bits</span>.<span class="cm-variable">dup</span> <span class="cm-operator">=</span> <span class="cm-variable">decode_msg</span>.<span class="cm-variable">header</span>.<span class="cm-variable">bits</span>.<span class="cm-variable">dup</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">encode_msg</span>.<span class="cm-variable">msg</span>.<span class="cm-variable">pubrel</span>.<span class="cm-variable">packet_id</span> <span class="cm-operator">=</span> <span class="cm-variable">decode_msg</span>.<span class="cm-variable">msg</span>.<span class="cm-variable">pubrec</span>.<span class="cm-variable">packet_id</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">/* publish callback, and delete callback */</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">qos2_publish_delete</span>(<span class="cm-variable">client</span>, <span class="cm-variable">encode_msg</span>.<span class="cm-variable">msg</span>.<span class="cm-variable">pubrel</span>.<span class="cm-variable">packet_id</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">/* delete array numbers! */</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">clear_one_pubrec_msg</span>(<span class="cm-variable">client</span>, <span class="cm-variable">encode_msg</span>.<span class="cm-variable">msg</span>.<span class="cm-variable">pubrel</span>.<span class="cm-variable">packet_id</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">umqtt_encode</span>(<span class="cm-variable">UMQTT_TYPE_PUBCOMP</span>, <span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">send_buf</span>, <span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">mqtt_info</span>.<span class="cm-variable">send_size</span>, <span class="cm-operator">&amp;</span><span class="cm-variable">encode_msg</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">if</span> (<span class="cm-variable">_ret</span> <span class="cm-operator">&lt;</span> <span class="cm-number">0</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">UMQTT_ENCODE_ERROR</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">LOG_E</span>(<span class="cm-string">" pubcomp failed!"</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">goto</span> <span class="cm-variable">exit</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">send_len</span> <span class="cm-operator">=</span> <span class="cm-variable">_ret</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">umqtt_trans_send</span>(<span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">sock</span>, <span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">send_buf</span>, <span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">send_len</span>, </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">mqtt_info</span>.<span class="cm-variable">send_timeout</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">if</span> (<span class="cm-variable">_ret</span> <span class="cm-operator">&lt;</span> <span class="cm-number">0</span>) </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">UMQTT_SEND_FAILED</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">LOG_E</span>(<span class="cm-string">" trans send failed!"</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">goto</span> <span class="cm-variable">exit</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">break</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">case</span> <span class="cm-variable">UMQTT_TYPE_PUBCOMP</span>:</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">LOG_D</span>(<span class="cm-string">" read pubcomp cmd information!"</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">rt_memset</span>(<span class="cm-operator">&amp;</span><span class="cm-variable">msg_ack</span>, <span class="cm-number">0</span>, <span class="cm-keyword">sizeof</span>(<span class="cm-variable">msg_ack</span>));</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">msg_ack</span>.<span class="cm-variable">msg_type</span> <span class="cm-operator">=</span> <span class="cm-variable">UMQTT_TYPE_PUBCOMP</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">msg_ack</span>.<span class="cm-variable">packet_id</span> <span class="cm-operator">=</span> <span class="cm-variable">decode_msg</span>.<span class="cm-variable">msg</span>.<span class="cm-variable">pubcomp</span>.<span class="cm-variable">packet_id</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">rt_mq_send</span>(<span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">msg_queue</span>, <span class="cm-operator">&amp;</span><span class="cm-variable">msg_ack</span>, <span class="cm-keyword">sizeof</span>(<span class="cm-keyword">struct</span> <span class="cm-def">umqtt_msg_ack</span>));</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">if</span> (<span class="cm-variable">_ret</span> <span class="cm-operator">!=</span> <span class="cm-variable">RT_EOK</span>) </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">UMQTT_SEND_FAILED</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">goto</span> <span class="cm-variable">exit</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">break</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">case</span> <span class="cm-variable">UMQTT_TYPE_SUBACK</span>:</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">LOG_D</span>(<span class="cm-string">" read suback cmd information!"</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">rt_memset</span>(<span class="cm-operator">&amp;</span><span class="cm-variable">msg_ack</span>, <span class="cm-number">0</span>, <span class="cm-keyword">sizeof</span>(<span class="cm-variable">msg_ack</span>));</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">msg_ack</span>.<span class="cm-variable">msg_type</span> <span class="cm-operator">=</span> <span class="cm-variable">UMQTT_TYPE_SUBACK</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">msg_ack</span>.<span class="cm-variable">packet_id</span> <span class="cm-operator">=</span> <span class="cm-variable">decode_msg</span>.<span class="cm-variable">msg</span>.<span class="cm-variable">suback</span>.<span class="cm-variable">packet_id</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">set_uplink_recon_tick</span>(<span class="cm-variable">client</span>, <span class="cm-variable">UPLINK_NEXT_TICK</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">rt_mq_send</span>(<span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">msg_queue</span>, <span class="cm-operator">&amp;</span><span class="cm-variable">msg_ack</span>, <span class="cm-keyword">sizeof</span>(<span class="cm-keyword">struct</span> <span class="cm-def">umqtt_msg_ack</span>));</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">if</span> (<span class="cm-variable">_ret</span> <span class="cm-operator">!=</span> <span class="cm-variable">RT_EOK</span>) </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">UMQTT_SEND_FAILED</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">goto</span> <span class="cm-variable">exit</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">break</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">case</span> <span class="cm-variable">UMQTT_TYPE_UNSUBACK</span>:</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">LOG_D</span>(<span class="cm-string">" read unsuback cmd information!"</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">rt_memset</span>(<span class="cm-operator">&amp;</span><span class="cm-variable">msg_ack</span>, <span class="cm-number">0</span>, <span class="cm-keyword">sizeof</span>(<span class="cm-variable">msg_ack</span>));</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">msg_ack</span>.<span class="cm-variable">msg_type</span> <span class="cm-operator">=</span> <span class="cm-variable">UMQTT_TYPE_UNSUBACK</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">msg_ack</span>.<span class="cm-variable">packet_id</span> <span class="cm-operator">=</span> <span class="cm-variable">decode_msg</span>.<span class="cm-variable">msg</span>.<span class="cm-variable">unsuback</span>.<span class="cm-variable">packet_id</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">set_uplink_recon_tick</span>(<span class="cm-variable">client</span>, <span class="cm-variable">UPLINK_NEXT_TICK</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">rt_mq_send</span>(<span class="cm-variable">client</span><span class="cm-operator">-&gt;</span><span class="cm-variable">msg_queue</span>, <span class="cm-operator">&amp;</span><span class="cm-variable">msg_ack</span>, <span class="cm-keyword">sizeof</span>(<span class="cm-keyword">struct</span> <span class="cm-def">umqtt_msg_ack</span>));</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">if</span> (<span class="cm-variable">_ret</span> <span class="cm-operator">!=</span> <span class="cm-variable">RT_EOK</span>) </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">UMQTT_SEND_FAILED</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">goto</span> <span class="cm-variable">exit</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">break</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">case</span> <span class="cm-variable">UMQTT_TYPE_PINGRESP</span>:</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">LOG_I</span>(<span class="cm-string">" ping resp! broker -&gt; client! now tick: %d "</span>, <span class="cm-variable">rt_tick_get</span>());</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">set_uplink_recon_tick</span>(<span class="cm-variable">client</span>, <span class="cm-variable">UPLINK_NEXT_TICK</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">break</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">default</span>:</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">LOG_W</span>(<span class="cm-string">" not right type(0x%02x)!"</span>, <span class="cm-variable">_pkt_type</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">break</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">exit</span>:</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">return</span> <span class="cm-variable">_ret</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 6693px;"></div><div class="CodeMirror-gutters" style="display: none; height: 6693px;"></div></div></div></pre><p><span>简述上面几个关键步骤：</span></p><ol start='' ><li><p><span>读Fixed header的第一个字节</span></p><p><span>这里会调用</span><code>umqtt_trans_recv</code><span>函数读取socket数据：</span></p><pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="C" style="break-inside: unset;"><div class="CodeMirror cm-s-inner cm-s-null-scroll CodeMirror-wrap" lang="c"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 9.5px; left: 8px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment">/** </span></span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* TCP/TLS receive datas on configured transport.</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">*</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* @param sock the input socket</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* @param recv_buf the output， receive datas buffer</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* @param buf_len the input, receive datas buffer length</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">*</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* @return &lt;=0: failed or other error</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* &nbsp; &nbsp; &nbsp; &nbsp; &gt;0: receive datas length</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">*/</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable-3">int</span> <span class="cm-def">umqtt_trans_recv</span>(<span class="cm-variable-3">int</span> <span class="cm-variable">sock</span>, <span class="cm-variable">rt_uint8_t</span> <span class="cm-operator">*</span><span class="cm-variable">recv_buf</span>, <span class="cm-variable">rt_uint32_t</span> <span class="cm-variable">buf_len</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">{</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">return</span> <span class="cm-variable">recv</span>(<span class="cm-variable">sock</span>, <span class="cm-variable">recv_buf</span>, <span class="cm-variable">buf_len</span>, <span class="cm-number">0</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-comment">// return read(sock, recv_buf, buf_len);</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">}</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-operator">-------------------------------------------------------------------------</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-meta">#define recv(s, mem, len, flags) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sal_recvfrom(s, mem, len, flags, NULL, NULL)</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 391px;"></div><div class="CodeMirror-gutters" style="display: none; height: 391px;"></div></div></div></pre><p><span>可以看到，该函数其实是对SAL层的</span><code>sal_recvfrom</code><span>函数做了一层封装，作用就是从对应的sock中读取buf_len长度的数据到recv_buf。</span></p></li><li><p><span>读Fixed header的Remaining length字段并解析剩余长度</span></p><p><span>这里就不赘述，该部分算法参照</span><a href='#213-remaining-length'><span>2.1.3 Remaining Length</span></a><span>的规则。</span></p></li><li><p><span>读剩余数据——可变header+有效载荷</span></p><p><span>不再赘述。</span></p></li><li><p><span>解析数据包，并根据不同报文类型做相应处理</span></p><p><span>这里有一个关键的结构体和解包函数：</span></p><ul><li><pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="C"><div class="CodeMirror cm-s-inner cm-s-null-scroll CodeMirror-wrap" lang="c"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 9.5px; left: 8px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation"><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">struct</span> <span class="cm-def">umqtt_msg</span> <span class="cm-def">decode_msg</span> <span class="cm-operator">=</span> { <span class="cm-number">0</span> };</span></pre></div></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 23px;"></div><div class="CodeMirror-gutters" style="display: none; height: 23px;"></div></div></div></pre><p><span>结构体成员可参考</span><strong><a href='#44-umqtt发送组包'><span>4.4. uMQTT发送组包</span></a></strong></p></li><li><pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="C" style="break-inside: unset;"><div class="CodeMirror cm-s-inner cm-s-null-scroll CodeMirror-wrap" lang="c"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 9.5px; left: 8px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment">/** </span></span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* parse the data according to the format</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">*</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* @param recv_buf the input, the raw buffer data, of the correct length determined by the remaining length field</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* @param recv_buf_len the input, the length in bytes of the data in the supplied buffer</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* @param message the output datas</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">*</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* @return &lt;0: failed or other error</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">* &nbsp; &nbsp; &nbsp; &nbsp; =0: success</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">*/</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable-3">int</span> <span class="cm-def">umqtt_decode</span>(<span class="cm-variable">rt_uint8_t</span> <span class="cm-operator">*</span><span class="cm-variable">recv_buf</span>, <span class="cm-variable-3">size_t</span> <span class="cm-variable">recv_buf_len</span>, <span class="cm-keyword">struct</span> <span class="cm-def">umqtt_msg</span> <span class="cm-operator">*</span><span class="cm-variable">message</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">{</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable-3">int</span> <span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-number">0</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">rt_uint8_t</span><span class="cm-operator">*</span> <span class="cm-variable">curdata</span> <span class="cm-operator">=</span> <span class="cm-variable">recv_buf</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">enum</span> <span class="cm-variable">umqtt_type</span> <span class="cm-variable">type</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">if</span> (<span class="cm-variable">message</span> <span class="cm-operator">==</span> <span class="cm-variable">RT_NULL</span>) </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">UMQTT_INPARAMS_NULL</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">LOG_E</span>(<span class="cm-string">" umqtt decode inparams null!"</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">goto</span> <span class="cm-variable">exit</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">message</span><span class="cm-operator">-&gt;</span><span class="cm-variable">header</span>.<span class="cm-variable">byte</span> <span class="cm-operator">=</span> <span class="cm-variable">umqtt_readChar</span>(<span class="cm-operator">&amp;</span><span class="cm-variable">curdata</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">type</span> <span class="cm-operator">=</span> <span class="cm-variable">message</span><span class="cm-operator">-&gt;</span><span class="cm-variable">header</span>.<span class="cm-variable">bits</span>.<span class="cm-variable">type</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">switch</span> (<span class="cm-variable">type</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">case</span> <span class="cm-variable">UMQTT_TYPE_CONNACK</span>:</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">umqtt_connack_decode</span>(<span class="cm-operator">&amp;</span>(<span class="cm-variable">message</span><span class="cm-operator">-&gt;</span><span class="cm-variable">msg</span>.<span class="cm-variable">connack</span>), <span class="cm-variable">recv_buf</span>, <span class="cm-variable">recv_buf_len</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">break</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">case</span> <span class="cm-variable">UMQTT_TYPE_PUBLISH</span>:</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">umqtt_publish_decode</span>(<span class="cm-variable">message</span>, <span class="cm-variable">recv_buf</span>, <span class="cm-variable">recv_buf_len</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">break</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">case</span> <span class="cm-variable">UMQTT_TYPE_PUBACK</span>:</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">umqtt_puback_decode</span>(<span class="cm-variable">message</span>, <span class="cm-variable">recv_buf</span>, <span class="cm-variable">recv_buf_len</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">break</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">case</span> <span class="cm-variable">UMQTT_TYPE_PUBREC</span>:</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">// _ret = umqtt_pubrec_decode();</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">break</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">case</span> <span class="cm-variable">UMQTT_TYPE_PUBREL</span>:</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">// _ret = umqtt_pubrel_decode();</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">break</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">case</span> <span class="cm-variable">UMQTT_TYPE_PUBCOMP</span>:</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">// _ret = umqtt_pubcomp_decode();</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">break</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">case</span> <span class="cm-variable">UMQTT_TYPE_SUBACK</span>:</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">umqtt_suback_decode</span>(<span class="cm-operator">&amp;</span>(<span class="cm-variable">message</span><span class="cm-operator">-&gt;</span><span class="cm-variable">msg</span>.<span class="cm-variable">suback</span>), <span class="cm-variable">recv_buf</span>, <span class="cm-variable">recv_buf_len</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">break</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">case</span> <span class="cm-variable">UMQTT_TYPE_UNSUBACK</span>:</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">_ret</span> <span class="cm-operator">=</span> <span class="cm-variable">umqtt_unsuback_decode</span>(<span class="cm-operator">&amp;</span>(<span class="cm-variable">message</span><span class="cm-operator">-&gt;</span><span class="cm-variable">msg</span>.<span class="cm-variable">unsuback</span>), <span class="cm-variable">recv_buf</span>, <span class="cm-variable">recv_buf_len</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">break</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">case</span> <span class="cm-variable">UMQTT_TYPE_PINGRESP</span>:</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">// _ret = umqtt_pingresp_encode();</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">break</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">default</span>:</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-keyword">break</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">exit</span>:</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">return</span> <span class="cm-variable">_ret</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 1380px;"></div><div class="CodeMirror-gutters" style="display: none; height: 1380px;"></div></div></div></pre><p><span>报文类型比较多，依然只拿</span><strong><span>CONNECT</span></strong><span>报文举例：</span></p><pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="C" style="break-inside: unset;"><div class="CodeMirror cm-s-inner cm-s-null-scroll CodeMirror-wrap" lang="c"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 9.5px; left: 8px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">static</span> <span class="cm-variable-3">int</span> <span class="cm-def">umqtt_connack_decode</span>(<span class="cm-keyword">struct</span> <span class="cm-def">umqtt_pkgs_connack</span> <span class="cm-operator">*</span><span class="cm-variable">connack_msg</span>, <span class="cm-variable">rt_uint8_t</span><span class="cm-operator">*</span> <span class="cm-variable">buf</span>, <span class="cm-variable-3">int</span> <span class="cm-variable">buflen</span>)</span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">{</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">MQTTHeader</span> <span class="cm-variable">header</span> <span class="cm-operator">=</span> {<span class="cm-number">0</span>};</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-variable-3">unsigned</span> <span class="cm-variable-3">char*</span> <span class="cm-variable">curdata</span> <span class="cm-operator">=</span> <span class="cm-variable">buf</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-variable-3">unsigned</span> <span class="cm-variable-3">char*</span> <span class="cm-variable">enddata</span> <span class="cm-operator">=</span> <span class="cm-variable">NULL</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-variable-3">int</span> <span class="cm-variable">rc</span> <span class="cm-operator">=</span> <span class="cm-number">0</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-variable-3">int</span> <span class="cm-variable">mylen</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-variable">header</span>.<span class="cm-variable">byte</span> <span class="cm-operator">=</span> <span class="cm-variable">umqtt_readChar</span>(<span class="cm-operator">&amp;</span><span class="cm-variable">curdata</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-keyword">if</span> (<span class="cm-variable">header</span>.<span class="cm-variable">bits</span>.<span class="cm-variable">type</span> <span class="cm-operator">!=</span> <span class="cm-variable">UMQTT_TYPE_CONNACK</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">rc</span> <span class="cm-operator">=</span> <span class="cm-variable">UMQTT_FAILED</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">LOG_E</span>(<span class="cm-string">" not connack type!"</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-keyword">goto</span> <span class="cm-variable">exit</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-variable">curdata</span> <span class="cm-operator">+=</span> (<span class="cm-variable">rc</span> <span class="cm-operator">=</span> <span class="cm-variable">umqtt_pkgs_decodeBuf</span>(<span class="cm-variable">curdata</span>, <span class="cm-operator">&amp;</span><span class="cm-variable">mylen</span>)); <span class="cm-comment">/* read remaining length */</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-variable">enddata</span> <span class="cm-operator">=</span> <span class="cm-variable">curdata</span> <span class="cm-operator">+</span> <span class="cm-variable">mylen</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-keyword">if</span> (<span class="cm-variable">enddata</span> <span class="cm-operator">-</span> <span class="cm-variable">curdata</span> <span class="cm-operator">&lt;</span> <span class="cm-number">2</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">LOG_D</span>(<span class="cm-string">" enddata:%d, curdata:%d, mylen:%d"</span>, <span class="cm-variable">enddata</span>, <span class="cm-variable">curdata</span>, <span class="cm-variable">mylen</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-keyword">goto</span> <span class="cm-variable">exit</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  }</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="" cm-zwsp="">
</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">connack_msg</span><span class="cm-operator">-&gt;</span><span class="cm-variable">connack_flags</span>.<span class="cm-variable">connack_sign</span> <span class="cm-operator">=</span> <span class="cm-variable">umqtt_readChar</span>(<span class="cm-operator">&amp;</span><span class="cm-variable">curdata</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">connack_msg</span><span class="cm-operator">-&gt;</span><span class="cm-variable">ret_code</span> <span class="cm-operator">=</span> <span class="cm-variable">umqtt_readChar</span>(<span class="cm-operator">&amp;</span><span class="cm-variable">curdata</span>);</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">exit</span>:</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-keyword">return</span> <span class="cm-variable">rc</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">}</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-operator">-----------------------------------------------------------------------------------------</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">union</span> <span class="cm-variable">umqtt_pkgs_connack_sign</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">{</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">rt_uint8_t</span> <span class="cm-variable">connack_sign</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">struct</span> {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">rt_uint8_t</span> <span class="cm-variable">sp</span>: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-number">1</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* current session bit */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-variable">rt_uint8_t</span> <span class="cm-variable">reserved</span>: &nbsp; &nbsp; &nbsp; <span class="cm-number">7</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cm-comment">/* retain bit */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;  } <span class="cm-variable">bits</span>;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">};</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">struct</span> <span class="cm-def">umqtt_pkgs_connack</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">{</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-comment">/* variable header */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">union</span> <span class="cm-variable">umqtt_pkgs_connack_sign</span> <span class="cm-variable">connack_flags</span>; &nbsp; &nbsp;<span class="cm-comment">/* connect flags */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-keyword">enum</span> <span class="cm-variable">umqtt_connack_retcode</span> <span class="cm-variable">ret_code</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="cm-comment">/* connect return code */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-comment">/* payload = NULL */</span> </span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">};</span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 1035px;"></div><div class="CodeMirror-gutters" style="display: none; height: 1035px;"></div></div></div></pre><p><span>仍旧是熟悉的套路：读Fixed header→读Remaining length→读Variable header解析相关flags</span></p></li></ul></li><li><p><span>UMQTT_TYPE_CONNACK:</span></p><p><span>调用</span><code>set_uplink_recon_tick(client, UPLINK_NEXT_TICK)</code><span>函数设置下一次重连滴答值，调用</span><code>set_connect_status(client, UMQTT_CS_LINKED)</code><span>函数设置uMQTT客户端状态为</span><code>已连接</code><span>。</span></p></li></ol><p><span>至此，已经完成了CONNECT报文的收发过程，下一步就是启动</span><code>umqtt_thread</code><span>线程，调用</span><code>umqtt_handle_readpacket</code><span>函数来处理从Broker服务端收到的数据报文。报文处理流程与上文类似，不再赘述，具体内容相关流程可参考图4 - 2：</span></p><p><img src="./umqtt.assets/mqtt流程图.png" referrerpolicy="no-referrer" alt="mqtt流程图"></p><center>图4 - 2	MQTT通信流程图</center><h3 id='46-umqtt数据流向'><strong><span>4.6. uMQTT数据流向</span></strong></h3><p><span>综上所述，使用一张流程图简要描绘uMQTT一些重要的函数调用，见图4 - 3。由于很多细节不好展现出来，还需要从实际的代码中体会其功能流程。</span></p><p><img src="./umqtt.assets/umqtt流程图.png" referrerpolicy="no-referrer" alt="umqtt流程图"></p><center>图4 - 3	umqtt重要函数流程图</center><p><span>此处加入部分重要函数的出现过的章节，便于跳转查看：</span></p><h4 id='461-umqtttransconnect-函数'><span>4.6.1 umqtt_trans_connect 函数</span></h4><p><a href='#43-umqtt与lwip'><span>点这里</span></a></p><h4 id='462-umqttencode-函数'><span>4.6.2 umqtt_encode 函数</span></h4><p><a href='#44-umqtt发送组包'><span>点这里</span></a></p><h4 id='463-umqtthandlereadpacket函数'><span>4.6.3 umqtt_handle_readpacket函数</span></h4><p><a href='#45-umqtt接收解包'><span>点这里</span></a></p><h2 id='5-扩展'><strong><span>5. 扩展</span></strong></h2><ol start='' ><li><p><span>感悟体验</span></p><p><span>MQTT协议是一种使用广泛、通用兼容性强的、适用于物联网的协议。市面上有着非常多的云端平台，可供我们便捷地搭建通信系统，无论是项目开发还是DIY玩耍，都能使用uMQTT软件包来快速实现需求，验证功能。</span></p></li><li><p><span>MQTT抓包分析</span></p><p><span>可以看</span><a href='https://www.cnblogs.com/hayasi/p/7761477.html'><span>这篇文章</span></a><span>，已经讲的很好很详细了，本文不再赘述。</span></p></li></ol></div></div>
</body>
</html>